文章目录
- 1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
- 2、 编写函数,实现判断是否无位置参数,如无参数,提示错误
- 3、编写函数实现两个数字做为参数,返回最大值
1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
#!/bin/bash
fun1 () {
if [ $1 -ge 1 ]
thenecho -e '\e[1;32mOK\e[m'
elseecho -e '\033[1;31mFAILED\033[m'
fi
}fun1 $#

2、 编写函数,实现判断是否无位置参数,如无参数,提示错误
fun2() {
if [ -z "$1" ]
then
echo "错误"
fi
}fun2 $1

3、编写函数实现两个数字做为参数,返回最大值
max1() {
read -p "first number :" num1
read -p "second number :" num2
if [ $num1 -ge $num2 ]
then
echo "max is $num1"
else
echo "max is $num2"
fi
}max1
