case条件语句相当于多分支的if/elif/else的条件语句,但是它比这些条件语句看起来更规范更工整,常被应用于实现系统服务启动脚本等企业应用场景中。

当case执行一个相匹配的表达式之后会跳出case的语句。

如果没有匹配变量任何值,则执行*)后面的指令,直到遇到双分号(;;)

case条件语句的语法格式为:

case “变量” in

   值1)

          指令...

          ;;

    值2)

           指令...

          ;;

     *)

        指令3...

esac

例如:

#!/bin/bash

read -p "plz input a num:" num

case "$num" in
  1)
    echo "the num is $num"
    ;;
  2)
    echo "the num is $num"
    ;;
  [3-9])
    echo "the num is $num"
    ;;
  *)
    echo "plz input [0-9] int"
esac

执行shell脚本,打印一个如下的水果菜单;

apple

pear

banana

cherry

#!/bin/bash
cat << EOF

  1.apple
  2.pear
  3.banana
  4.cherry
EOF

read -p "plz input your choice:" hh

case "$hh" in
  1)
    echo -e "\E[1;31m apple\E[0m"
    ;;
  2)
    echo -e "\E[1;33m pear\E[0m"
    ;;
  3)
    echo -e "\E[1;34m banana\E[0m"
    ;;
  4)
    echo -e "\E[1;35m cherry\E[0m"
    ;;
  *)
    echo -e "\E[1;36m plz inpit num\E[0m"
esac
~

这里有加颜色的字符用法:


echo  -e  "\E[1;31m char\E[0m" #\E可以使用\033代替,数字1表示加粗,31表示红色,其他颜色使用3[2-7]表示,[0m表示结束

脚本里面还可以这样写

#!/bin/bash

red="\E[1;31m"
green="\E[1;32m"
yellow="\E[1;33m"
res="\E[0m"
echo -e "$red hongse"$res""
echo -e "$green chengse"$res""
echo -e "$yellow huangse"$res""

如何要调整背景颜色将上面的1修改为40~47


结合case语句给输出的字符串加颜色

要求:使用case语句及通过脚本传入指定的内容和指定颜色,在脚本命令行传入两个参数,并给指定颜色;

#!/bin/bash

bb() {
    echo "plz input two arge"
    exit 3
}
aa() {
 red="\E[43;31m"
 green="\E[41;32m"
 yellow="\E[46;33m"
 res="\E[0m"
#echo -e "$red hongse"$res""
#echo -e "$green chengse"$res""
#echo -e "$yellow huangse"$res""

case "$2" in
  red|RED)
    echo -e "$red$1$res"
    ;;
  green|GREEN)
    echo -e "$green$1$res"
    ;;
  yellow|YELLOW)
    echo -e "$yellow$1$res"
    ;;
  *)
    echo "plz input (rea|green|yellow)"
esac
}

main() {
 if [ $# -ne 2 ]; then
   bb
 fi
aa $1 $2
}
main $*

image.png

范例9-8:已知Nginx Web服务的管理命令如下

启动脚本命令为/application/nginx/sbin/nginx

停止脚本命令为/application/nginx/sbin/nginx -s nginx

请用case语句开发脚本,以实现nginx服务启动及关闭功能,具体脚本命令为/etc/init.d/nginx {start|stop|restart},并实现通过chkconfig进行开机自启动的管理。


由于ubuntu里面没有/etc/init.d/functions这个文件,所以没加

#!/bin/bash

aa=/etc/init.d/nginx
path=/usr/sbin
if [ ! -f "/etc/init.d/nginx" ]; then
  echo "plz install nginx"
  exit 3
fi
#if [ "$#" -eq 0 -o "$#" -eq 2 ]; then
#  echo "this is good"
#else
#  exit 4       
#fi

start() {
  if [ "`ps -ef | grep nginx | wc -l`" -gt 2 ]; then
    echo "the nginx  service is starting"
    return 0
  else
    $path/nginx
    aa=$?
    if [ $aa -eq 0 ]; then
      echo "nginx  is started"   
    else
      echo "nginx is started"  
    fi
  fi

}
stop() {
 if [ "`ps -ef | grep nginx | wc -l`" -eq 1 ]; then
   echo "the nginx is stopped"
   return 0

 else
   $path/nginx -s stop
   bb=$?
   if [ $bb -eq 0 ]; then
     echo  "nginx is stopped"  
   else
     echo "nginx is  stopped"  

   fi
 fi
}
case "$1" in
  start)
    start
    ss=$?
    echo  $ss
    ;;
  stop)
    stop
    ss=$?
    echo  $ss
    ;;
  restart)
    stop
    sleep 2
    start
    ss=$?
    echo $ss
    ;;
  *)
    echo "this is not"
esac