大家好,今天给大家带来的是Shell编程之case语句的使用!

其实case语句还是很好理解的,在shell编程中,if语句有它的语法,函数也有它的语法,那么在我们shell编程中的case语句也是有它的语法的,如下:

case 变量名 in 
  值1) 
   指令1 
  ;; 
  值2) 
   指令2 
  ;; 
  值3) 
   指令3 
  ;; 
esac 

在以上的代码中我们发现,以case开头,以esac结尾,就像我们的if语句,以if开头以fi结束时一样的。

例2:

case "找老公条件" in 
   家里有房子) 
    嫁给你... 
  ;; 
   家庭有背景) 
    嫁给你... 
  ;; 
   很努力吃苦) 
    那么我们可以先谈男女朋友.. 
  ;; 
esac 

好了接下来我们实践,其实语法就是上面的一些基本语法,那么我么在实践中来体会case到底有什么用

其实自我认为case语句更适合一些菜单选项的脚本,那么我们先用if语句写一个菜单脚本如下:

#!/bin/bash 
#!/bin/bash 
# Date: 4:42 2018-2-5 
# Mail: ywyankerp@163.com 
# Founder: <YanKai> 
# Describe: This is a one - button installation service script  
function CDAN(){ 
cat << yankerp  
1.米饭 
2.面条 
3.包子 
yankerp 
} 
CDAN 
read -p 请您输入您今天要吃什么: NUM 
expr $NUM + 1 &>/dev/null 
if [ "$?" -ne 0 ] 
 then 
  echo "请您输入{1|2|3}"  
  exit 1 
fi 
 
if [ "$NUM" -eq 1 ] 
  then 
   echo "小二,来一碗米饭" 
elif [ $NUM -eq 2 ] 
  then 
   echo "小二,来一碗面条" 
elif [ "$NUM" -eq 3 ] 
  then 
   echo "小二,来一锅包子" 
fi 


首先我们看到我们上面的if语句,是不是感觉有点繁琐,那么接下来我们使用case语句来实现 如下:

#!/bin/bash 
#!/bin/bash 
# Date: 4:42 2018-2-5 
# Mail: ywyankerp@163.com 
# Founder: <YanKai> 
# Describe: This is a one - button installation service script  
function CDAN(){ 
cat << yankerp  
1.米饭 
2.面条 
3.包子 
yankerp 
} 
CDAN 
read -p 请您输入您今天要吃什么: NUM 
expr $NUM + 1 &>/dev/null 
if [ "$?" -ne 0 ] 
 then 
  echo "请您输入{1|2|3}"  
  exit 1 
fi 
 
case $NUM in 
    1) 
     echo "小二,来一碗米饭" 
    ;; 
    2) 
     echo "小二,来一碗面条" 
    ;; 
    3) 
     echo "小二,来一锅包子" 
    ;; 
esac 


剖析时刻:

#!/bin/bash 
#!/bin/bash 
# Date: 4:42 2018-2-5 
# Mail: ywyankerp@163.com 
# Founder: <YanKai> 
# Describe: This is a one - button installation service script  
function CDAN(){  #定义一个CDAN的函数 
cat << yankerp    
1.米饭 
2.面条 
3.包子 
yankerp 
} 
CDAN       #调用CDAN函数 
read -p 请您输入您今天要吃什么: NUM  #输入一条提示,然后把用户输入的字符串赋值给变量NUM 
expr $NUM + 1 &>/dev/null     # 使用数值运算命令expr来确定用户输入的是否是数值 
if [ "$?" -ne 0 ]         #如果用户输入的不是数值 
 then      #那么 
  echo "请您输入{1|2|3}"   #打印出 1 2 3  
  exit 1       #最后退出脚本 
fi        #结束if语句 
 
case $NUM in    #这里就是我们的case语句了,简单理解,在NUM这个变量中 
    1)       #如果用户输入的是1 
     echo "小二,来一碗米饭"    #那么就输出一行来一碗米饭 
    ;; 
    2)             #如果在NUM变量中用户输入的是2 
     echo "小二,来一碗面条"  #那么继续打印此内容 
    ;; 
    3)              #以此类推 
     echo "小二,来一锅包子" 
    ;; 
esac       #最后使用esac结束 

觉得不够养眼?没有关系如下图:


我们发现,在多个选项的这些脚本中呢,还是使用case语句是比较方便合适的。

那么我们掌握了基本的语法和它的一些功能,那么我们可以结合函数+case语句来完成一个菜单式的安装脚本如下:

#!/bin/bash 
# Date: 4:42 2018-2-5 
# Mail: ywyankerp@163.com 
# Founder: <YanKai> 
# Describe: This is a one - button installation service script  
function CDAN(){ 
cat << yankerp  
+------------------------------------------------+ 
|                        | 
|    _o0o_     1. 安装Nginx       | 
|    08880     2. 安装Apache      | 
|    88"."88     3. 安装MySQL       | 
|    (|-_-|)     4. 安装PHP        | 
|    0\=/0     5. 部署LNMP环境     | 
|   __/  \__    6. 安装zabbix监控    | 
|   ‘\  ///‘     7. 退出此管理程序    | 
|  / Linux一键 \   8. 关闭计算机      | 
| ||  Server  ||  ======================  |  
| \    ////     一键安装服务      | 
|  ||| i i i  |||        by Yankerp  | 
|  ___    ___   ======================  | 
|___‘. /--.--\ .‘___              | 
+------------------------------------------------+ 
yankerp 
} 
CDAN 
. /etc/init.d/functions 
BASE_DIR=/usr/local/src 
read -p "请您输入1-8任意字符:" NUM 
expr $NUM + 1 &>/dev/null 
if [ "$?" -ne 0 ] 
 then 
  echo "请您输入{1|2|3|4|5|6|7|8}" 
  exit 1 
fi 
 
function Nginx_server (){ 
  echo "开始安装Nginx请稍后...." 
  yum install -y gcc gcc-c++ pcre-devel zlib-devel openssl-devel &>/dev/null 
  cd $BASE_DIR && wget http://nginx.org/download/nginx-1.12.2.tar.gz &>/dev/null && useradd -M -s /sbin/nologin nginx && \ 
tar zxf nginx-1.12.2.tar.gz && cd $BASE_DIR/nginx-1.12.2/ && \  
  ./configure --prefix=/usr/local/nginx --with-http_dav_module--with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module--with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx &>/dev/null 
  if [ "$?" -eq 0 ] 
   then 
    make && make install &>/dev/null 
  fi 
if [ -f /usr/local/nginx/sbin/nginx ];then 
  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin && nginx && echo "Nginx安装成功!" 
fi 
} 
       
function Apache_server (){ 
  echo "开始安装Apache请稍后" 
  yum install httpd -y &>/dev/null 
  if [ "$?" -eq 0 ] 
   then  
    echo "Apache安装成功!" 
   else 
    echo "Apache安装失败!" 
  fi 
} 
 
case $NUM in 
  1) 
   Nginx_server 
  ;; 
  2) 
   Apache_server 
  ;; 
esac 

运行结果如下:


以上脚本大家可以参考参考,很简单实现,那么接下来在来一个案例

实战2:编程Nginx启动脚本

编译安装的Nginx是没有启动脚本的,这时候我们可以选择自己写一个,在这里我用我的思路给大家解释整个过程虽然不是很规范,希望还是能够帮助到你如下:

#!/bin/bash 
BASE_DIR=/usr/local/nginx/sbin/ 
PID=/usr/local/nginx/logs/nginx.pid 
#no1.判断是否root用户登录 
if [ "$UID" -ne 0 ] 
 then 
  echo "请您使用root用户登录!!!" 
 exit 1 
fi 
. /etc/init.d/functions  
#判断用户输入的传参个数 
if [ "$#" -ne 1 ] 
  then 
    echo "/bin/bash $0 {start|stop|restart}" 
  exit 1 
fi 
 
#编写启动Nginx函数 
function start (){ 
  if [ `netstat -anput | grep nginx | wc -l` -eq 0 ] 
    then 
     $BASE_DIR/nginx 
  fi 
    if [ "$?" -eq 0 ] 
     then 
      action "Nginx启动成功!" /bin/true 
     else 
      action "Nginx启动失败!" /bin/false 
    fi 
} 
 
#编写停止Nginx函数 
function stop (){ 
  if [ `netstat -anput | grep nginx | wc -l` -ne 0 ] 
    then 
     $BASE_DIR/nginx -s stop 
  fi 
    if [ "$?" -eq 0 ] 
     then 
      action "Nginx停止成功!" /bin/true 
     else 
      action "Nginx停止失败!" /bin/false 
    fi 
} 
 
#编写case语句 使用特殊的位置变量$1来判断用户输入的内容 
case $1 in 
  start) 
     start 
  ;; 
  stop) 
     stop 
  ;; 
    restart) 
     stop 
     start 
  ;; 
esac 

运行如下:


一张图更清晰解释


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部