linux shell编程中case语句
1、举例1:
root@PC1:/home/test# ls test.sh root@PC1:/home/test# cat test.sh #!/bin/bash read -p "please input a character: " KEY case $KEY in ## 利用变量KEY进行匹配 [a-z]|[A-Z]) echo "letter!" ;; [0-9]) echo "number!" ;; *) ## 兜底匹配 echo "space, function key or other control character!" esac root@PC1:/home/test# bash test.sh please input a character: 8 number! root@PC1:/home/test# bash test.sh please input a character: f letter! root@PC1:/home/test# bash test.sh please input a character: # space, function key or other control character!
2、举例2 ubuntu中监测Apache服务(没有太大意义)
root@liujiaxinpc1:/home/test# cat test.sh #!/bin/bash case $1 in start) systemctl start apache2 echo -e "\033[1;41;33m now the status is : \033[0m" /etc/init.d/apache2 status ;; stop) systemctl stop apache2 echo -e "\033[1;41;33m now the status is : \033[0m" /etc/init.d/apache2 status ;; restart) systemctl restart apache2 echo -e "\033[1;41;33m now the status is : \033[0m" /etc/init.d/apache2 status ;; status) echo -e "\033[1;41;33m now the status is : \033[0m" /etc/init.d/apache2 status ;; *) echo "usage:bash $0 start|stop|restart|status" ;; esac
参考:https://mp.weixin.qq.com/s?__biz=MzA3ODA3MjAxNg==&mid=2247500245&idx=1&sn=fed50ff9d24816fa840ab259c03523da&chksm=9f4ad06aa83d597cafcce8da8897bec95e3342b2a64c43d0ba70265715ef39625571049a97e8&mpshare=1&scene=23&srcid=0505U5CKzW20nkWpHzeqtmhr&sharer_sharetime=1651685548817&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-05-06 c语言 6-1
2021-05-06 第一个java程序
2021-05-06 c语言中自定义函数计算x的n次方
2021-05-06 c语言中while语句控制程序循环的次数