1.编写函数,实现打印绿色OK和红色FAILED
判断是否有参数,存在为Ok,不存在为FAILED
1 ###############################################
2 # Author: lee
3 # Version:
4 # Create_Time: 2023/03/20
5 # Mail: lee@westos.org
6 # Info:
7 #
8 ################################################
9
10 #!/bin/bash
11
12 print(){
13 if [ $# -eq 0 ]
14 then
15 echo -e "\033[32m OK \033[0m"
16 else
17 echo -e "\033[31m FAILED \033[0m"
18 fi
19 }
20
21 print $1

2. 编写函数,实现判断是否无位置参数,如无参数,提示错误
1 ###############################################
2 # Author: lee
3 # Version:
4 # Create_Time: 2023/03/20
5 # Mail: lee@westos.org
6 # Info:
7 #
8 ################################################
9
10 #!/bin/bash
11
12 print(){
13 if [ $# -ne 0 ]
14 then
15 echo "参数为$1"
16 else
17 echo "错误:无位置参数"
18 fi
19 }
20
21 print $1

3. 编写函数实现两个数字做为参数,返回最大值
###############################################
# Author: lee
# Version:
# Create_Time: 2023/04/02
# Mail: lee@westos.org
# Info:
#
################################################
#!/bin/bash
fun(){
if [ $x -gt $y ]
then
echo "最大值为:$x"
elif [ $x -eq $y ]
then
echo "x=y"
else
echo "最大值为:$y"
fi
}
read -p "please input two numbers:" x y
fun $x $y
-----------------------------------------结果如下--------------------------------------
[root@master node1]# sh zy1.sh
please input two numbers:12 4
最大值为:12
[root@master node1]# sh zy1.sh
please input two numbers:2 2
x=y
[root@master node1]# sh zy1.sh
please input two numbers:1 3
最大值为:3