当前位置:网站首页>shell 函数和数组练习
shell 函数和数组练习
2022-07-27 06:50:00 【空丶白灬】
1、编写函数,实现打印绿色OK和红色FAILED
判断是否有参数,存在为Ok,不存在为FAILED
#!/bin/bash
func()
{
echo $#
if [ -n "$1" ];then #-n 后的参数需要" "
echo -e "\033[32m OK \033[0m"
else
echo -e "\033[31m failed \033[0m"
fi
}
func $1
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash
func()
{
echo $#
if [ -n "$1" ];then #-n 后的参数需要" "
echo "can shu wei $1"
else
echo -e "error"
fi
}
func $1
3、编写函数实现两个数字做为参数,返回最大值
#!/bin/bash
compare()
{
if [ $1 -gt $2 ] ; then
return $1
elif [ $1 -lt $2 ] ; then
return $2
else
echo "same"
fi
}
compare $1 $2
echo "$?"
4、编写函数,实现两个整数位参数,计算加减乘除。
#!/bin/bash
calculator()
{
if expr $1 + $2 >/dev/null ; then
echo "$1+$2= $(($1+$2))"
echo "$1-$2= $(($1-$2))"
echo "$1*$2= $(($1*$2))"
echo "$1/$2= $(($1/$2))"
else
echo "not zhengshu"
fi
}
calculator $1 $2
5、将/etc/shadow文件的每一行作为元数赋值给数组
#!/bin/bash
user=($(cat /etc/shadow))
for ((i=0;i<${#user[*]};i++));
do
echo ${user[$i]}
done
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
#!/bin/bash
declare -A a
num=`wc -l </etc/passwd`
for ((i=1;i<=$num;i++));
do
b[$((i-1))]=$(head -$i /etc/passwd | tail -1 )
done
for ((i=0;i<${#b[*]};i++));
do
type=`echo ${
b[$i]} | awk -F: '{print $NF}'`
let a[$type]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
#2
for i in `cut -d: -f7 /etc/passwd
do
let a[$i]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
7、使用关联数组按扩展名统计指定目录中文件的数量
#!/bin/bash
declare -A a
b=(`ls -l /shell/test | awk '{print $NF}'`)
for ((i=0;i<${#b[*]};i++));
do
type=`echo ${
b[$i]}|grep [a-z] | awk -F. '{print $NF}'`
let a[$type]++
done
for i in ${
!a[*]}
do
echo "$i : ${a[$i]}"
done
边栏推荐
- sql语句批量更新 时间减去1天
- vlan间路由(讲解+验证)
- IO中节点流和处理流的理解学习
- 【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
- Codeforces Round #810 (Div.2) A-C
- flink去重(一)flink、flink-sql中常见的去重方案总结
- C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)
- The solution of using sqlplus to display Chinese as garbled code
- 我是不是被代码给耽误了……不幸沦为一名程序员……
- ARP broadcasting practice cases
猜你喜欢

Using docker in MAC to build Oracle database server

drawImage方法第一次调用不显示图片的解决方式

C common function integration-3

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

js正则表达式实现每三位数字加一个逗号

C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)

Array method and loop in JS

Temperature and humidity measurement and display device based on Arduino

基于Arduino的温度、湿度测量显示装置

(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
随机推荐
Clickhouse notes 1 | introduction, features | installation and use based on centos7 system | common data types | mergetree table engine | SQL operation
正则表达式基础整理
Okaleido ecological core equity Oka, all in fusion mining mode
Use Amazon dynamodb and Amazon S3 combined with gzip compression to maximize the storage of player data
MySQL backup strategy
Guava的基础功能与集合
tigervnc的使用
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
Haikang H9 camera cannot be connected with xshell (SSH is not enabled)
Oracle cleans up the Database disk space of tables with referenced partitions
ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
Routing between VLANs (explanation + verification)
oracle的触发器的使用举例
vlan间路由(讲解+验证)
在kettle使用循环来处理表中的数据
User unlock sm04 sm12
杂谈:跟女儿聊为啥要学好文化课
Use Popen to execute a command and get the return result
functools模块
Temperature and humidity measurement and display device based on Arduino