shell补-循环案例-循环case
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]#
分类:
shell实战2查漏补缺
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!