shell补-循环案例-循环case

发布时间 2023-12-15 11:10:51作者: WeChat2834
shell补-循环案例-循环case

case结构条件的语法格式;一般应用菜单的功能

case $变量名 in
"值1")
###如果变量的值1,则执行此
程序1
;;
"值2")
###如果变量的值2,则执行此
程序2
;;
###....省略其他分支
YES|yes|Yes)       #####此处的值,可以带些符号,比如|表示或者;也可以用[a-z]感觉可以正则
###
程序3
;;
*)
###如果变量的值不是以上的值,则执行此
程序4
;;;
esac
[root@localhost myfunction]# cat testfunction3.sh 
#!/bin/bash
[ -f /root/tmp/myfunction/myfunctiontest2.sh ] && source  /root/tmp/myfunction/myfunctiontest2.sh ||exit
if [ "$#" -ne 1 ]; then
	errmsg
	exit 1
fi
case "${1}" in
"start")	
	startRsync
	exit 0
;;
"stop")
	stopRsync
	exit 0
;;
"restart")

	restartRsync
	exit 0
;;
*)
	errmsg
;;
esac
[root@localhost myfunction]# bash testfunction3.sh rr r rq 
启停rsync脚本,你只能录入{start|stop|restart}
[root@localhost myfunction]# bash testfunction3.sh start
rsync服务启动成功
[root@localhost myfunction]# bash testfunction3.sh stop
rsync服务已停止
[root@localhost myfunction]# bash testfunction3.sh restart
stopvar:0--------------startvar :2
rsync服务已经重启成功
[root@localhost myfunction]# bash testfunction3.sh 3  3
启停rsync脚本,你只能录入{start|stop|restart}
#######################################################################
#!/bin/bash

read -p  "请录入一个字符" var

case  "${var}" in
 
 [a-Z] )
	echo “"你录入的是一个字母"
 ;;
 [0-9])
	echo "你录入的是一个数字"
 ;;
 *)
	echo "你录入的是一个符号"
esac
[root@localhost myfunction]# bash testcase.sh  
请录入一个字符1
你录入的是一个数字
[root@localhost myfunction]# bash testcase.sh  
请录入一个字符a
“你录入的是一个字母
[root@localhost myfunction]# bash testcase.sh  
请录入一个字符@
你录入的是一个符号
[root@localhost myfunction]#