1. 编写函数,实现打印绿色OK和红色FAILED
判断是否有参数,存在为Ok,不存在为FAILED
[root@localhost ~]# vim test1.sh
#!/bin/bash
pri(){
if [ $# -ne 0 ]
then
echo -e "\033[32m OK \033[0m"
else
echo -e "\033[31m FAILED \033[0m"
fi
}
[root@localhost ~]# sh test1.sh l
OK
[root@localhost ~]# sh test1.sh
FAILED
2. 编写函数,实现判断是否无位置参数,如无参数,提示错误
[root@localhost ~]# vim test2.sh
#!/bin/bash
pan (){
if [ $# -eq 0 ]
then
echo "无位置参数error"
else
echo "位置参数为:$1"
fi
}
pan $1
[root@localhost ~]# sh test2.sh a
位置参数为:a
[root@localhost ~]# sh test2.sh
无位置参数error
3. 编写函数实现两个数字做为参数,返回最大值
[root@localhost ~]# vim test3.sh
#!/bin/bash
read -p "input two num:" x y
max (){
if [ $x -gt $y ]
then
echo "max=$x"
else
echo "max=$y"
fi
}
max $x $y
[root@localhost ~]# sh test3.sh
input two num:16 56
max=56