1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为OK,不存在为FAILED
#!/bin/bash
test1() {
if [ $# -ne 0 ];then
echo -e "\033[32m OK \033[0m"
else
echo -e "\033[31m FAILED \033[0m"
fi
}
read -p "请输入参数:" i
test1 $i
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash
test2(){
fir_num=$1
loc_num=$2
if [ $fir_num -ge $loc_num ]
then
echo "存在位置参数"
else
echo "错误,无位置参数"
fi
}
read -p "请输入参数:" i
test2 $# $i
3、编写函数实现两个数字做为参数,返回最大值
#!/bin/bash
test3(){
if [ $a -gt $b ]
then
echo "最大值为:$a"
elif [ $a -eq $b ]
then
echo "a=b"
else
echo "最大值为:$b"
fi
}
read -p "请输入两个数字:" a b
test3 $a $b
4、编写函数,实现两个整数位参数,计算加减乘除。
#!/bin/bash
read -p "请输入两个整数位参数:" a b
test4(){
echo $a+$b=$((a+b))
echo $a-$b=$((a-b))
echo $a*$b=$((a*b))
echo $a/$b=$((a/b))
}
test4 $a $b
5、将/etc/shadow文件的每一行作为元数赋值给数组
#!/bin/bash
num=`wc -l < /etc/shadow`
for i in `seq $num`
do
arr[$i]=`head -$i /etc/shadow | tail -1`
done
for i in ${arr[*]}
do
echo $i
done
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
#!/bin/bash
declare -A array
while read line
do
type=`echo $line | cut -d : -f7`
let array[$type]++
done </etc/passwd
for i in ${!array[@]}
do
echo "$i : ${array[$i]}"
done
7、使用关联数组按扩展名统计指定目录中文件的数量
#!/bin/bash
declare -a array=`ls`
declare -A dect
for i in ${array[@]}
do
let dect[${i#*.}]++
done
echo ${!dect[@]}
for j in ${!dect[@]}
do
echo $j : ${dect[$j]}
done