1. 程式人生 > >15、bash編程、case、函數

15、bash編程、case、函數

lscpu form pac 模式匹配 啟動 信息 ash opp ice

1、bash腳本編程:

選擇執行:if單分支、if雙分支、if多分支;case語句


2、case語句:

語法格式:

case $VARIABLE in //$VARIABLE是變量引用,in是關鍵字

PAT1) //模式1

分支1

;; //以雙分號結束分支1語句

PAT2)

分支2

;;

...

*) //所有都不匹配,為*,類似else

分支n

;;

esca

註意:①、每個case中的模式語句必須以雙分號結尾,這裏的雙分號可以單獨成行,也可以放在每個分支的最後一條語句後面,PAT僅支持golb風格的通配符(*、?、[ ] 、[^ ])

②、case語句僅適用於一個變量與多個值分別做模式匹配時適用。



3、示例:顯示一個如下菜單給用戶

cpu) display cpu information

mem)display mem information

disk)display disk information

*) quit

要求:提示用戶給出自己的選擇

正確的選擇則給出相應的信息,否則,則提示重新選擇正確的選項。

if語句實現:

[root@localhost sh]# cat men.sh

#!/bin/bash

cat << EOF

cpu) display cpu information

mem)display mem information

disk)display disk information

*) quit

EOF


read -p "enter your choice:" choice


while [ "$choice" != "cpu" -a "$choice" != "mem" -a "$choice" != "disk" -a "$choice" != "*" ];do

echo "please make sure your choice"

read -p "enter your choice again:" choice

done


if [ "$choice" == "cpu" ];then

lscpu

elif [ "$choice" == "mem" ];then

free

elif [ "$choice" == "disk" ];then

fdisk -l /dev/[hs]d[a-z]

else

echo "quit"

exit 4

fi

[root@localhost sh]#

case語句實現:

[root@localhost sh]# cat case.sh

#!/bin/bash

cat << EOF

cpu) display cpu information

mem)display mem information

disk)display disk information

*) quit

EOF


read -p "enter your choice:" choice


while [ "$choice" != "cpu" -a "$choice" != "mem" -a "$choice" != "disk" -a "$choice" != "*" ];do

echo "please make sure your choice"

read -p "enter your choice again:" choice

done


case $choice in

cpu)

lscpu;;

mem)

free

;;

disk)

fdisk -l /dev/[hs]d[a-z]

;;

*)

echo "quit"

exit 4

esac

[root@localhost sh]#


4、示例:寫一個服務腳本:$lockfile 值:/var/lock/subsys/SCRIPT_NAME

要求:此腳本可接受start、stop、restart、status四個參數之一

如果參數非此四者,則提示使用幫助後退出

start,則創建lockfile,並顯示啟動

stop,則刪除lockfile,並顯示停止

restart,則先刪除此文件,再創建此文件,而後顯示重啟完成

status,如果lockfile存在,則顯示running,否則,則顯示為stopped

[root@localhost init.d]# cat testservice

#!/bin/bash

#chkconfig: - 50 50

#description: test service script

prog=$(basename $0)

lockfile=/var/lock/subsys/$prog

case $1 in

start)

if [ -f $lockfile ];then

echo "$prog is running yet"

else

touch $lcokfile

[ $? -eq 0 ] && echo "start $rpog finished"

fi

;;

stop)

if [ -f $lockfile ];then

rm -rf $lockfile

[ $? -eq 0 ] && echo "stop $prog finished"

else

echo "prog is not runing"

fi;;

restart)

if [ -f $lockfile ];then

rm -rf $lockfile

touch $lockfile

echo "restart $prog finished"

else

touch -f $lockfile

echo "start $prog finished"

fi;;

status)

if [ -f $lockfile ];then

echo "$prog is running"

else

echo "$prog is stopped"

fi;;

*)

echo "usage:$prog {start | stop | restart | status}"

exit 5;;

esac

[root@localhost init.d]#


測試:

[root@localhost init.d]# cp testservice.sh /etc/init.d/

[root@localhost init.d]# chkconfig --add testservice.sh

[root@localhost init.d]# chkconfig --list testservice.sh

[root@localhost init.d]# chmod +x /etc/init.d/testservice.sh

[root@localhost init.d]# service testservice.sh start

[root@localhost init.d]# ls /var/lock/subsys/ //每個啟動的服務在此目錄下都有一個鎖文件。





5、函數:function;對於過程式編程來講,函數是實現代碼重用的主要組件之一。


5.1、函數的定義:把一段獨立功能的代碼當做一個整體,並為它命名一個名字,即命名的代碼段。註意:函數的代碼段不會自己執行,它只能在被調用時才執行。


5.2、函數的調用:在代碼中給定函數名即可;函數名出現的問題,在代碼執行時都會被自動的替換為函數代碼。


5.3、函數的功能:

①實現模塊化編程

②實現結構化編程


5.4、函數的語法:

語法一:


function f_name{

函數體

}


註意:function是關鍵字,f_name是函數名,函數體在bash中編寫的各類代碼,無論是順序的、選擇的、循環的,都可以放在函數體中。

語法二:


f_name( ) {

函數體

}


註意:函數都有自己的生命周期:每次被調用時創建,返回時終止。

函數也有返回值:其狀態返回結果為函數體中運行的最後一條命令的狀態結果


5.5、自定義函數狀態返回值:return

return用法:

return [0-255]

0:表示成功

1-255:表示失敗

註意:函數中遇到return命令時,函數就不再往下運行了。


腳本的狀態返回值:exit

exit用法:

exit [0-255]

0:表示成功

1-255:表示失敗


5.6、函數示例:

給定一個用戶名,通過腳本取得用戶ID,和默認shell



























15、bash編程、case、函數