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