成都网站建设设计

将想法与焦点和您一起共享

shell脚本基础知识梳理<五>:流程控制case

系统运维

case使用格式

十年的泰州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整泰州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“泰州网站设计”,“泰州网站推广”以来,每个客户项目都认真落实执行。

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


本文标题:shell脚本基础知识梳理<五>:流程控制case
文章位置:http://chengdu.cdxwcx.cn/article/cpjops.html