天天看點

Shell程式設計入門(第二版)(下)

三、select/in[較少用]

格式:

    select [變量] in [關鍵字]  

    do   

        command 1   

        ... ...   

        command n   

    done   

#select把關鍵字中的每一項做成類似表單,以互動的方式執行do和done之間的指令  

示例-select.sh

#!/bin/bash  

# Show select usage  

echo "What's your favorate OS?"  

select var in "Linux" "Windows" "UNIX" "Other"  

do  

    break  

done  

echo "You have selected $var"  

Shell程式設計入門(第二版)(下)

四、case/esac

case 變量 in   

字元串1)   

    指令清單1   

    ;;   

    ...   

字元串n)   

    指令清單n   

esac  

示例-case.sh

# Show Usage for case/esac  

echo "*********************************"  

echo "Please select a oprator as below:"  

echo "C ... copy"  

echo "D ... delete"  

echo "B ... backup"  

read op  

case $op in  

    C)    

        echo "copy...."  

        ;;    

    D)    

        echo "delete...."  

    B)    

        echo "backup..."  

        ;;  

    *)  

        echo "Unknow operator!"  

        exit 1  

示例-select.case

# A test shell script for select and case  

echo "a is 5, b is 3, please select your method"  

a=5  

b=3;  

select var in "a+b" "a-b" "a*b" "a/b"  

case $var in  

    "a+b")  

        echo "a+b="`expr $a + $b `  

    "a-b")  

        echo "a-b="`expr $a - $b`  

    "a*b")  

        echo "a*b="`expr $a \* $b`  

    "a/b")  

        echo "a/b="`expr $a / $b`  

        echo "input error..."  

執行個體-/etc/rc.d/init.d/httpd部分源代碼

# See how we were called.  

case "$1" in  

  start)  

    start  

    ;;  

  stop)  

    stop  

  status)  

  restart)  

    ;;    

  condrestart|try-restart)  

    if status -p ${pidfile} $httpd >&/dev/null; then  

        stop  

        start  

    fi  

  force-reload|reload)  

        reload  

  graceful|help|configtest|fullstatus)  

    $apachectl $@  

    RETVAL=$?  

  *)  

    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"  

    RETVAL=2  

Shell程式設計入門(第二版)(下)

五、while

while 條件    #無限:while true  

do   

    指令   

done   

示例-while.sh

# A usage for while  

num=1  

while [ $num -le 10 ]  

    echo $(expr $num \* $num)  

    let num++  

示例-useradd.sh

# A shell script to add user(s)  

#echo 123456 | passwd --stdin xiaofang  #用非互動方式設定xiaofang的密碼  

echo -n "Plese input the user name: "  

read username  

echo -n "Plese input the sum users: "  

read sum  

while [ $num -le $sum ]  

    /usr/sbin/useradd "$username$num"     

    if [ $? -ne 0 ]   

    then  

        echo "user: $username already exists."  

    fi    

echo -n "Please input the passwd for this users: "  

read passwd  

i=1  

while [ $i -le $sum ]  

    echo $passwd | /usr/bin/passwd --stdin "$username$i"  

    let i++  

Shell程式設計入門(第二版)(下)

示例-userdel.sh

# A shell script for delete user(s)  

echo -n "Please input the username: "  

echo -n "Please input the user number: "  

read num   

while [ $i -le $num ]  

    /usr/sbin/userdel -r $username$i  

        echo "User: $username$i is not exists."  

        let i++   

        continue  

    let i++   

六、until

格式:

    until 條件   

        指令   

#until類似while循環,不同的是until是條件傳回值為假時才繼續執行。  

示例-until.sh

# A script to show until usage.  

echo "Please input Y/y to stop..."  

read input  

until [ "$input" = "Y" ] || [ "$input" = "y" ]  

    echo "input error, input again!"  

    read input  

七、跳出循環:break和continue 

break:跳出整個循環 

continue:跳過本次循環,進行下次循環

示例-break_continue.sh

# A test shell script for break&continue  

while true  

    echo "*****************************"  

    echo "Please have a select as blow:"  

    echo "1 Copy"  

    echo "2 Delete"  

    echo "3 Backup"  

    echo "4 Quit***********************"  

    read op  

    case $op in  

        "1")  

            echo "$op is Copy"  

            ;;    

        "2")  

            echo "$op is Delete"  

        "3")  

            echo "$op is Backup"  

            ;;  

        "4")  

            echo "Exit..."  

            break  

        "*")  

            echo "Invalide selectino, please select again..."  

            continue  

    esac  

八、shift指令

參數左移,每執行一次,參數序列順次左移一個位置,$#的值減1, 用于分别處理每個參數,移出去的參數不再可用

示例-shift.sh

# A test shell script for shift  

if [ $# -lt 1 ]   

then  

    echo "No enough parameters"  

    exit 1  

fi  

num=0  

while [ $# -gt 0 ]   

    echo '$1 is '$1  

    shift  

echo $num  

執行個體-/etc/rc.d/init.d/httpd中的start源代碼

Shell程式設計入門(第二版)(下)

一、函數的定義: 

函數名 ()   

{   

    指令序列   

}   

二、函數的調用:不帶() 

函數名 參數1 參數2 ... 參數n

執行個體-調用

Shell程式設計入門(第二版)(下)

三、函數中的變量: 

變量均為全局變量,沒有局部變量

四、函數中的參數:

調用函數時,可以傳遞參數,在函數中用$1、$2...來引用 

示例-function.sh

# A test shell script for function  

# function  

Help(){  

    echo "Usage: sh function \$1 \$2 \$3"  

}  

Display(){  

    echo "three argument: $1 $2 $3"  

# main  

if [ $# -ne 3 ]   

    Help  

else  

    echo "Think you for your input"  

    Display $1 $2 $3  

sh -x script  這将執行該腳本并顯示所有變量的值。 

sh -n script  不執行腳本隻是檢查文法的模式,将傳回所有文法錯誤。 

最佳實踐-指令最好使用絕對路徑

一個腳本能夠執行-

1.對腳本有rx權限,隻有r,可以使用sh執行

2.對腳本所在目錄至少有rx權限

拓展執行個體-setuid.sh

# After the system installed, please check setuid files first for security  

# mkdir /backup  

# find / -perm -4000 -o -perm -2000 > /backup/setuid.list  

/bin/find / -perm -4000 -o -perm -2000 > /tmp/setuid.list 2> /dev/null  

for var in `/bin/cat /tmp/setuid.list`  

    /bin/grep $var /backup/setuid.list > /dev/null 2> /dev/null  

        echo "$var is not in /backup/setuid.list, It's danger!"  

done