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

 

posted @   小鲨鱼2018  阅读(193)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源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语句控制程序循环的次数
点击右上角即可分享
微信分享提示