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]#

相关内容

热门资讯

我来教教您“桂麻圈.有没有挂?... 网上科普关于“桂麻圈有没有挂”话题很是火热,小编也是针对桂麻圈作*弊开挂的方法以及开挂对应的知识点,...
最新引进“畅游贵州麻将.怎么装... 有 亲,根据资深记者爆料畅游贵州麻将是可以开挂的,确实有挂(咨询软件无需...
我来教教您“九线拉王.怎么开挂... 家人们!今天小编来为大家解答九线拉王透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
终于懂了“新世界牛牛.辅助器?... 终于懂了“新世界牛牛.辅助器?”其实是有挂您好,新世界牛牛这个游戏其实有挂的,确实是有挂的,需要了解...
玩家攻略科普“笑傲江湖.可以开... 您好:笑傲江湖这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
玩家最新攻略“家乡大贰.开挂神... 网上科普关于“家乡大贰有没有挂”话题很是火热,小编也是针对家乡大贰作*弊开挂的方法以及开挂对应的知识...
今日重大发现“新大圣炸金花.怎... 您好:新大圣炸金花这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款...
【第一消息】“欢聚斗地主.有没... 您好:欢聚斗地主这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游...
我来教教您“新大圣炸金花.是不... 您好:新大圣炸金花这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款...
终于了解“开心游戏.到底有挂吗... 网上科普关于“开心游戏有没有挂”话题很是火热,小编也是针对开心游戏作*弊开挂的方法以及开挂对应的知识...