RHCE的一道题:
要求:
当运行 /root/script foo,输出为 bar
当运行 /root/script bar,输出为 foo
当没有任何参数或者参数不是 foo 或者 bar时, 其错误输出产生以下的信息:
/root/script foo|bar
[root@localhost ~]# more script #!/bin/bash # author ninesun # date 2021年5月31日22:40:37 # RHCE shell 题目 case $1 in foo) echo "bar" ;; bar) echo "foo" ;; *) echo "/root/script foo|bar" ;; esac
t ~]# ./script fo /root/script foo|bar [root@localhost ~]# [root@localhost ~]# ./script foo bar [root@localhost ~]# ./script bar foo
case语法:
你会经常发现自己在尝试计算一个变量的值,在一组可能的值中寻找特定值。在这种情形下,
你不得不写出很长的if-then-else语句,就像下面这样。
cat test25.sh #!/bin/bash # looking for a possible value # if [ $USER = "rich" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "barbara" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "testing" ] then echo "Special testing account" elif [ $USER = "jessica" ] then echo "Do not forget to logout when you're done" else echo "Sorry, you are not allowed here" fi $ $ ./test25.sh Welcome rich Please enjoy your visit $ elif语句继续if-then检查,为比较变量寻找特定的值。 有了case命令,就不需要再写出所有的elif语句来不停地检查同一个变量的值了。 case命 令会采用列表格式来检查单个变量的多个值。 case variable in pattern1 | pattern2) commands1;; pattern3) commands2;; *) default commands;; esac case命令会将指定的变量与不同模式进行比较。如果变量和模式是匹配的,那么shell会执行 为该模式指定的命令。可以通过竖线操作符在一行中分隔出多个模式模式。星号会捕获所有与已 知模式不匹配的值。这里有个将if-then-else程序转换成用case命令的例子。 $ cat test26.sh #!/bin/bash # using the case command # case $USER in rich | barbara) echo "Welcome, $USER" echo "Please enjoy your visit";; testing) echo "Special testing account";; jessica) echo "Do not forget to log off when you're done";; *) echo "Sorry, you are not allowed here";; esac $ $ ./test26.sh Welcome, rich Please enjoy your visit $
--------------------------2021年7月15日09:00:17
一道case 的例子,redis官方的启动命令脚本
redis_init_script.sh
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. ### BEGIN INIT INFO # Provides: redis_6379 # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Redis data structure server # Description: Redis data structure server. See https://redis.io ### END INIT INFO REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF & fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
2021年12月24日23:08:44 更新
这也是一个很好的例子
#!/bin/bash #打印菜单 while : do echo "********************" echo " menu " echo "1.tima and date" echo "2.system info" echo "3.uesrs are doing" echo "4.exit" echo "********************" read -p "enter you choice [1-4]:" choice #根据客户的选择做相应的操作 case $choice in 1) echo "today is `date +%Y-%m-%d`" echo "time is `date +%H:%M:%S`" read -p "press [enter] key to continue..." Key #暂停循环,提示客户按enter键继续 ;; 2) uname -r read -p "press [enter] key to continue..." Key ;; 3) w read -p "press [enter] key to continue..." Key ;; 4) echo "Bye!" exit 0 ;; *) echo "error" read -p "press [enter] key to continue..." Key ;; esac done
2021年12月28日15:01:07
select 结合 case的一个案例:
#!/bin/bash PS3="your choice is[5 for exit]:" select choice in disk_partion filesys cpu_load mem_util quit;do case "$choice" in disk_partion) fdisk -l ;; filesys) df -h ;; cpu_load) uptime ;; mem_util) free -m ;; quit) break ;; *) echo "error" exit esac done
如果没有PS3 的定义选择按钮前的处处比较丑。
有PS3定义的结果: