shell脚本基础知识梳理<五>:流程控制case
admin
2023-03-12 15:22:17
0

case使用格式

case 值 in
模式1)
指令1
指令2
...
;;
模式2)
指令1
指令2
...
;;
esca
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
1) echo '你选择了 1'
;;
2) echo '你选择了 2'
;;
3) echo '你选择了 3'
;;
4) echo '你选择了 4'
;;
*) echo '你没有输入 1 到 4 之间的数字'
;;
esac

实例 1

#!/bin/bash
#case...esac
echo "What is your preferred scripting language?"
echo "1) bash"
echo "2) prel"
echo "3) python"
echo "4) ruby"
echo "5) I do not know !"
read lang
case $lang in
1) echo "you selected bash" ;;
2) echo "you selected prel" ;;
3)
echo "you selected python"
;;
4)
echo "you selected ruby"
;;
5)
echo "I do not know!"
;;
esac

执行结果

[root@localhost shell]# sh case.sh
What is your preferred scripting language?
1) bash
2) prel
3) python
4) ruby
5) I do not know !
4
you selected ruby
[root@localhost shell]# sh case.sh
What is your preferred scripting language?
1) bash
2) prel
3) python
4) ruby
5) I do not know !
5
I do not know!
[root@localhost shell]#

实例 2

#!/bin/bash
echo -n "Do you agree whith this? [yes or no]: "
read yn
case $yn in
[Yy] | [Yy][Ee][Ss])
echo "Agreed."
;;
[Nn] | [Nn][Oo])
echo "Not Agreed."
exit 1
;;
*)
echo "Invalid input."
;;
esac

运行结果:

[root@localhost shell]# sh case2.sh
Do you agree whith this? [yes or no]: 1
Invalid input.
[root@localhost shell]# sh case2.sh
Do you agree whith this? [yes or no]: n
Not Agreed.
[root@localhost shell]# sh case2.sh
Do you agree whith this? [yes or no]: Yes
Agreed.
[root@localhost shell]#

实例 3

#!/bin/bash
case $1 in
sql) echo "Runing mysql backup using mysqldump tool..."
;;
sync) echo "Runing backup using rsyuc tool..."
;;
git) echo "Runing backup using gistore tool..."
;;
tar) echo "Runing tape backup using tar tool..."
;;
*)
echo "Backup shell script utility"
echo "Usage: $0 {sql|sync|git|tar}"
echo " sql : Run mysql backup utility."
echo " sync : Run web backup utility."
echo " git : Run gistore backup utility."
echo " tar : Run tape backup utility."
;;
esac

运行结果:

[root@localhost shell]# sh case3.sh
Backup shell script utility
Usage: case3.sh {sql|sync|git|tar}
sql : Run mysql backup utility.
sync : Run web backup utility.
git : Run gistore backup utility.
tar : Run tape backup utility.
[root@localhost shell]# sh case3.sh sql
Runing mysql backup using mysqldump tool...
[root@localhost shell]#

实例 4

#!/bin/bash
#得到磁盘使用率最高的
max_usage=$(df -Ph |awk '{print $5}'|grep %|grep -v "Use"|sort -n|tail -1|cut -d "%" -f1)
max_dir=df -Ph | awk '{print $5,$6}'|grep -v Use|sort -n|tail -1|awk '{print $2}'
case ${max_usage} in
[1-6])
MSG="All is quiet.\"${max_dir}\" is ${max_usage}% used"
;;
[7-8]
)
MSG="Start thinking about cleaning out some stuff. "
MSG="$MSG There's a partition ${max_dir} is ${max_usage}% used."
;;
9[1-8])
MSG="Better hurry with that new disk... "
MSG="$MSG One partition ${max_dir} is ${max_usage}% used."
;;
99)
MSG="I'm drowning herel There's a partiton at ${max_usage}% used"
;;
)
MSG="I seem to be runing with an nonexitent amount of disk space..."
;;
esac
#echo $MSG| mail -s "disk report date" root
echo $MSG

运行结果:

[root@localhost shell]# sh case4.sh
All is quiet."/" is 22% used
[root@localhost shell]#

相关内容

热门资讯

官方回应博主浦东机场打车遭抛客 7月25日,知名博主“花式足球克里斯”世界杯观赛后回国,在浦东机场打车,遭遇出租车司机谎称车辆故障中...
女孩正颌手术被做反,主导医生称... 近日,武汉。24岁女孩小雨仅2毫米轻微地包天,本可正畸改善,却被建议做正颌手术,原定下颌后移2毫米,...
台风“红霞”逼近,揭阳全力筑牢... 7月25日9时,省防汛防旱防风总指挥部将防风Ⅲ级应急响应提升为防风Ⅱ级应急响应。台风“红霞”正以25...
原创 王... 王虹夺得数学界的诺贝尔奖——菲尔兹奖。她的导师、母校、同学等等,都成为了全网讨论的对象。 而她的导师...
电池技术对二手车残值影响大吗? 随着新能源汽车市场的迅猛扩张,潜在购车者在关注新车性能的同时,也越来越重视后续流通环节的价值表现。其...
韩国一监狱百发子弹下落不明,已... 【环球网报道 记者 姜蔼玲】据韩联社援引韩国法律界7月26日消息,针对大田一所监狱100发子弹下落不...
Anthropic发布Opus... Anthropic正式发布Claude Opus 5人工智能模型。官方宣称,该模型在智能水平接近前沿...
从菲尔兹奖看广东的“育才密码” □李妹妍 7月23日,2026年国际数学家大会传来喜讯,中国籍学者邓煜、王虹双双摘得国际数学界最高荣...
神舟二十三号乘组太空之旅满两月... 神二十三号乘组朱杨柱、张志远、黎家盈在轨已满两月,空间站内各项科学实(试)验进展顺利。上周,三名航天...
紧张关系进一步升级,巴西外交部... 【环球网报道】美国《纽约时报》当地时间25日报道称,据美巴两国官员透露,巴西政府阻止了一个美国代表团...