1. 程式人生 > >shell腳本編程學習筆記-case語句

shell腳本編程學習筆記-case語句

linux shell

1.case結構條件語句語法

case語句實際上就是規範的多分支if語句

case “字符串變量”in

值1)指令1…

;;

值2)指令2…

;;

*)指令3…

esac

中文編程語法:

case “找女朋友條件”in

有房)嫁給你…

;;

你爸是李剛)嫁給你…

;;

努力吃苦)可以考慮先談朋友…

;;

*)good bye!!!

esac

2.簡單case腳本

輸入1、2、3分別輸出對應的值

[root@shellbiancheng jiaobenlianxi]# cat case01.sh 
#!/bin/bash
usage() {
echo "USAGE:$0 {1|2|3}" contents
exit 1
}

num() {
case "$1" in
1)echo "1"
;;
2)echo "2"
;;
3)echo "3"
;;
*)usage
esac
}

main() {
    if [ $# -ne 1 ];then
    usage   
    fi  
    num $1
}

main $*

3.執行腳本打印一個水果菜單如下:

a.apple

b.pear

c.banana

d.cherry

當用戶選擇水果的時候,打印告訴它選擇的水果是什麽並給選擇的水果加上顏色。要求case語句實現。

[root@shellbiancheng jiaobenlianxi]# cat menufruit.sh 
#!/bin/bash
RED_COLOR=‘\E[1;31m‘
GREEN_COLOR=‘\E[1;32m‘
YELLOW_COLOR=‘\E[1;33m‘
BLUE_COLOR=‘\E[1;34m‘
PINK=‘\E[1;35m‘
SHAN=‘\E[31;5m‘  提示閃爍功能結合 echo –e 使用
RES=‘\E[0m‘
menu(){
cat <<EOF
 1.[ apple ]
 2.[ pear ]
 3.[ bananan ]
 4.[ cherry ]
 5.exit
EOF
    read -p "Please input a fruit:" fruit
}

usage(){
    echo -e ${SHAN}"please select the exitnum behind"${RES} 
    echo "==========================================="
}

fruit(){
case "$fruit" in
    1)echo -e "${RED_COLOR} apple $RES"
;;
    2)echo -e "${GREEN_COLOR} pear $RES"
;;
    3)echo -e "${YELLOW_COLOR} bananan $RES"
;;
    4)echo -e "${BLUE_COLOR} cherry $RES"
;;
    5)
    exit
;;
    *)usage
esac
}
main(){
    while true
    do
        menu
        fruit
    done
}
Main

4.作業

已知nginx管理命令為:

啟動:/usr/local/nginx/sbin/nginx

停止:/usr/local/nginx/sbin/nginx –s stop

重新加載:/usr/local/nginx/sbin/nginx –s reload

請用case腳本模擬nginx服務啟動關閉:

/etc/init.d/nginx {start|stop|restart|reload}

並實現可通過chkconfig管理。

實戰操作:

其實很簡單,我們可以分四個模塊第一個模塊是啟動服務模塊,第二個模塊是關閉服務模塊,第三個模塊是重啟服務模塊,第四個模塊是平滑重啟模塊。先用函數分別實現這四個模塊,在調用這些函數即可,最後就是設置開機自啟,這就需要用chkconfig命令了。首先需要將我們寫的啟動腳本放到/etc/init.d/下面,然後在放到開機自啟動下面。

我們先把啟動腳本重命名,然後放到/etc/init.d/下面並給執行權限。

[root@shellbiancheng jiaobenlianxi]# cp nginx.sh nginx
[root@shellbiancheng jiaobenlianxi]# cp nginx /etc/init.d/
[root@shellbiancheng jiaobenlianxi]# chmod +x /etc/init.d/nginx 
[root@shellbiancheng jiaobenlianxi]# ll /etc/init.d/nginx 
 -rwxr-xr-x. 1 root root 981 4月   2 04:16 /etc/init.d/nginx

添加開機自啟動

在添加開機自啟動之前我們需要知道服務的服務自啟動一般在哪個運行級別。
以network為例,查看network服務開機啟動運行級別

[root@shellbiancheng jiaobenlianxi]# chkconfig --list network
network 0:關閉    1:關閉    2:啟用    3:啟用    4:啟用    5:啟用    6:關閉

等級:

0:關機;

1:單用戶模式;

2:多用戶模式,沒有NFS;

3:標準多用戶模式;

4:不可用;

5:X11,圖形界面模式;

6:重啟。

上面是在命令行查看network的開機啟動運行級別,下面我們在network啟動腳本查看,network的開機自啟動級別。

[root@shellbiancheng jiaobenlianxi]# head -5 /etc/init.d/network 
#! /bin/bash
#
# network   Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \

我們看上面的chkconfig這一行,這個很重要它定義了開機的啟動順序默認都是2345,10代表的是服務開機啟動順序,90代表的是服務的關機啟動順序我們將chkconfig和description這一行 復制到/etc/init.d/下的nginx的啟動腳本中。我們先去/etc/rc.d/rc3.d/裏面找一下沒有用到的開機啟動順序,我們看S20是沒有的我們就將nginx的開機啟動順序設為20。關閉順序同理也是一樣的,這裏就不演示了關機順序以大寫K開頭,我們將nginx的關機順序設為16。

[root@shellbiancheng logs]# ll /etc/rc.d/rc3.d/ |grep S19
lrwxrwxrwx. 1 root root 17 12月 30 04:10 S19rpcgssd -> ../init.d/rpcgssd
[root@shellbiancheng logs]# ll /etc/rc.d/rc3.d/ |grep S20

腳本代碼如下:

[root@shellbiancheng jiaobenlianxi]# cat nginx.sh 
#!/bin/bash
# chkconfig: 2345 20 16
# description: nginx is a http server
#Date: 2018-04-07 
#Author: Create by linzhongniao
#Mail: [email protected] 
#Function:This scripts function is Nginx startup script.
#Version: 1.1  

if [ -f /etc/init.d/functions ];then
     . /etc/init.d/functions
fi
pidfile=/usr/local/nginx/logs/nginx.pid
SHAN=‘\E[31;5m‘
RES=‘\E[0m‘
nginx=/usr/local/nginx/sbin/nginx
RETVAL=0
linzhongniao() {
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
      action "Nginx is $1" /bin/true
    else
      action "Nginx is $1" /bin/true
    fi
}

start() {
    if [ -f $pidfile ];then
        echo -e ${SHAN}"nginx is running"${RES}
    else
        $nginx
        linzhongniao started    
    fi
    return $RETVAL
}
stop() {
    if [ ! -f $pidfile ];then
        echo -e  ${SHAN}"nginx is stopped"${RES}
    else 
        $nginx -s stop 
        linzhongniao stopped
    fi
    return $RETVAL
}

restart() {
    printf "Restarting Nginx ...\n" 
    stop
    sleep 2
    start
}

reload() {
    if [ ! -f $pidfile ];then
        echo -e ${SHAN}"Can‘t open $pidfile,no such file or directory"${RES}
    else 
        $nginx -s reload
        linzhongniao reload
    fi
    return $RETVAL
}

usage() {
    echo -e ${SHAN}"USAGE:$0 {start|stop|restart|reload}"${RES} 
}

main() {
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  *)usage
    exit $RETVAL
    esac
}
main $1
exit $RETVAL

最後我們把它加載到chkconfig裏面,完成nginx服務開機自啟動

[root@shellbiancheng init.d]# chkconfig nginx on
[root@shellbiancheng init.d]# chkconfig --list nginx
nginx   0:關閉    1:關閉    2:啟用    3:啟用    4:啟用    5:啟用    6:關閉
關閉開機自啟動
[root@shellbiancheng init.d]# chkconfig nginx off
[root@shellbiancheng init.d]# chkconfig --list nginx
nginx   0:關閉    1:關閉    2:關閉    3:關閉    4:關閉    5:關閉    6:關閉

5.case語句小結

(1)case語句相當於多分支的if語句。case語句優勢更規範、易讀。

(2)case語句適合變量值少,並且固定的數字或字符串的集合。(1,2,3)或(start,stop,restart)。

(3)系統服務啟動腳本傳參多使用case語句,參考/etc/init.d/rsyslog的啟動腳本。

(4)所有case語句都可以使用if實現,但是case語句更規範清晰一些。

(5)case語句一般適合於服務的啟動腳本。

(6)case的變量的值如果已知固定的start/restart/stop的元素比較適合。

語句小結:

(1)case主要是寫啟動腳本,範圍較窄。

(2)if取值判斷、比較、應用廣泛。

6.學習系統腳本

多向系統腳本學習

/etc/init.d/functions

函數庫functions詳解:http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html

/etc/rc.d/rc.sysinit

/etc/init.d/rpcbind

/etc/init.d/nfs

/etc/init.d/httpd

shell腳本編程學習筆記-case語句