运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi



运维Shell脚本小试牛刀(一)

运维Shell脚本小试牛刀(二)

[运维Shell脚本小试牛刀(三)::(cd (dirname 0); pwd)命令详解](https://blog.csdn.net/u014635374/article/details/132579373 "运维Shell脚本小试牛刀(三)::(cd (dirname 0); pwd)命令详解")

运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客


一: if...elif...elif..else fi多层嵌套

root@www dicfor\]# cat compareNumbers.sh #!/bin/bash - #================================================================================================================== # # # FILE: compareNumbers.sh # USAGE: ./compareNumbers.sh # DESCRIPTION: 演示逻辑与 \&\& 和 逻辑或 \|\| # OPTIONS: ------- # REQUIREMENTS: --------- # # BUGS: ------ # NOTES: -------- # AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com # ORGANIZATION: # CREATED: 2023-8-29 09:11:20 # REVISION: -------- # # # # # #==================================================================================================================== # 如果指定的命令行参数个数不为1 ,则打印脚本的使用方法信息并退出脚本的运行,退出状态码为1 if \[ $# -ne 1

then

打印脚本的使用信息

echo "Uage: 'basename $0' number"

退出脚本,状态码为1

exit 1

fi

将第一个命令行参数赋值给变量num

num=$1

如果$num值大于90 且小于100,则执行if语句的内容,否则执行elif的语句

if [ "num" -ge 90 \] \&\& \[ num -le 100 ]

then

打印Excellent

echo "Excellent......................!"

如果$num的值大于80 且小于 90, 则执行次elif语句中的内容, 否则执行下面的elif语句

elif [ num -ge 80 \] \&\& \[ num -lt 90 ]

then

打印Good...

echo "Good !............................................"

#如果大于等于60且小于80,则执行elif语句中的内容,否则执行下面的elif语句块,

elif [ num -ge 60 \] \&\& \[ {num} -lt 80 ]

then

打印"Pass mark!"

echo "Pass mark!......................................"

elif [ num -lt 60 \] \&\& \[ num -ge 0 ]

then

打印"Fail !"

echo "Fail!............................................"

如果上面的if条件和elif 条件语句都没有执行,则执行次else语句块

else

打印 "Wrong number........"

echo "Wrong number......................................"

fi


测试输出:

root@www dicfor\]# ./compareNumbers.sh 2 Fail!............................................ \[root@www dicfor\]# ./compareNumbers.sh 2 334 Uage: 'basename ./compareNumbers.sh' number \[root@www dicfor\]# ./compareNumbers.sh 2 100 Uage: 'basename ./compareNumbers.sh' number \[root@www dicfor\]# ./compareNumbers.sh 2 90 Uage: 'basename ./compareNumbers.sh' number \[root@www dicfor\]# ./compareNumbers.sh 90 Excellent......................!

#!/bin/bash -

#==================================================================================================================

FILE: checknumber.sh

USAGE: ./checknumber.sh

DESCRIPTION:

OPTIONS: -------

REQUIREMENTS: ---------

BUGS: ------

NOTES: --------

AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com

ORGANIZATION:

CREATED: 2023-8-24 09:11:20

REVISION: --------

#====================================================================================================================

如果指定的命令行参数个数等于0,则显示必须指定一个参数的提示信息,然后退出脚本,退出状态码为1

if [ $# -eq 0 ]

then

显示必须输入一个参数的提示信息

echo "$0 : You must give/supply one integers...."

退出脚本,退出状态码为1

exit 1;

fi

如果指定的参数大于0

if [ $1 -gt 0 ]

then

echo "The number is positive........"

如果指定的参数小于0

elif [ $1 -lt 0 ]

then

echo "The number is negative........"

如果指定的参数等于0

elif [ $1 -eq 0 ]

then

echo "The number is zero............"

else

echo "Opps! $1 is not number,please enter a number..."

fi


测试输出:

root@www dicfor\]# ./checknumber.sh hhd ./checknumber.sh: 第 34 行:\[: hhd: 期待整数表达式 ./checknumber.sh: 第 40 行:\[: hhd: 期待整数表达式 ./checknumber.sh: 第 46 行:\[: hhd: 期待整数表达式 Opps! hhd is not number,please enter a number... \[root@www dicfor\]# ./checknumber.sh 2 The number is positive........ \[root@www dicfor\]# ./checknumber.sh -1111111111111 The number is negative........ \[root@www dicfor\]# ./checknumber.sh 0 The number is zero..........

相关推荐
都给我18 分钟前
服务器中涉及节流(Throttle)的硬件组件及其应用注意事项
服务器·网络·express
long_mingyue1 小时前
CVE-2022-46169漏洞复现
linux
吹个口哨写代码1 小时前
防止包含 XSS 攻击风险的内容提交成功
java·服务器·前端
默|笙1 小时前
【Linux】基本指令(2)
linux·运维·服务器
ZY小袁2 小时前
MGRE综合实验
服务器·网络·笔记·网络安全·学习方法·信息与通信·p2p
一位搞嵌入式的 genius2 小时前
暑期自学嵌入式——Day10(C语言阶段)
linux·笔记·学习·嵌入式c语言
被遗忘的旋律.2 小时前
Linux驱动开发笔记(五)——设备树(上)
linux·驱动开发·笔记
conkl5 小时前
构建 P2P 网络与分布式下载系统:从底层原理到安装和功能实现
linux·运维·网络·分布式·网络协议·算法·p2p
求知若渴,虚心若愚。6 小时前
Error reading config file (/home/ansible.cfg): ‘ACTION_WARNINGS(default) = True
linux·前端·ansible
π大星星️7 小时前
Nginx 四层(stream)反向代理 + DNS 负载均衡
运维·nginx·负载均衡