shell 1

文章展示了如何使用Bash脚本创建函数来处理命令行参数。第一个函数检查是否存在参数,如果存在则打印绿色的OK,否则打印红色的FAILED。第二个函数确保至少有一个位置参数,并在没有参数时给出错误提示。第三个函数接收两个数字作为参数,返回其中的最大值。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值