20 天學會bash shell script (三)

pingley發表於2012-04-09
20 天學會bash shell script. (三)
bash shell 中的true and false
在bash shell 中true 用0 表示,false 用非0 表示。
if語句
if 語句的作用是用於程式中做判斷。語法格式:
if condition
then 
   comand1
else
   comand2
if
這裡的condition 可以是一個比較表示式,命令的退出狀態等。如果condition 
為true 執行command1,condition 為false 執行 command2.
下面一個小shell script. 使用if 語句判斷一個檔案是否存在於使用者的home中。
該if 語句使用了退出狀態作為判斷的condition。
[linux@zeng bin]$ cat ifcondition.sh 
#!/bin/bash
#
#This shell script. use for demo if ...then statement.
#
if [ -f ~/bin/math.sh ]#注意這裡的空格是怎麼留的。
then
    echo "math.sh already exist in you home."
else
    echo "math.sh no found."
fi
[linux@zeng bin]$ ifcondition.sh 
math.sh already exist in you home.
你會發現上面的shell script. 寫的很死,沒什麼擴充套件性,我們稍作修改成下面的樣子。
[linux@zeng bin]$ cat ifcondition.sh
#!/bin/bash
#
#This shell script. use for demo if ...then statement.
#
if test -f $1
then
    echo "$1 already exist in you home."
else
    echo "$1 no found."
fi
經過修改現在的ifcondition.sh 強大多了。這裡要提下指令碼的風格
要注意做適當的縮排提高shell script. 的可讀性。
[linux@zeng bin]$ ifcondition.sh  ~/.bash_history
/home/linux/.bash_history already exist in you home.
[linux@zeng bin]$ ifcondition.sh  xxx.sh
xxx.sh no found.
test 命令
test 命令經常和if 語句一起使用用來做true/false 的判斷。test 命令有兩種形式
第一種是:test expression,第二種是:[ expression ]
當test 命令的執行結果為true 的時候退出狀態是0,否則是1.
常用的test 選項有:
用於檔案測試或者目錄測試的有
-d file  如果file 是一個資料夾。
-e file  如果file 存在為真。
-f  file  如果file 存在並且是普通檔案為真。
-L file  如果file 存在並且是符號連線為真。
-x file  如果file 對你是可讀的為真。
-w file  如果file 對你是讀寫的為真。
-x file   如果file 對你是可執行的為真。
file1 -nt file2 如果file1 的修改時間比file2 新為真。
file1 -ot file2 如果file2 的修改時間比file2 老為真。
用作算數運算的有
-eq 等於,-ne 不等於,-lt 小於,le 小於或者等於,-gt 大於,-ge 大於等於。
用作邏輯操作的運算子有
! expression 邏輯not
expression1 -a expression2 邏輯and
expression1 -o expression2 邏輯or
用作字串比較的有
string1 = string2 ,如果string1 等於string2 返回真。
string1 != string2,如果string1 不等於string2 返回真。
string1 ,表示string1 不為空,或者沒有定義。
-n string1,表示string1 不為空並且已經存在。
-z string1,表示string1 是空的或者已經存在。
下面這個小 shell script. 用來測試執行指令碼使用者的身份,並且呼叫我們
上面寫好的ifcondition.sh,身份的判斷使用了test 命名中的字串比較。
[linux@zeng bin]$ id -u
500
[linux@zeng bin]$ cat test_linux.sh 
#!/bin/bash
#
#This shell script. for test linux.
#

if [ $(id -u) != "500" ]
then 
    echo "You must be the linux user to run this script"
    exit 1
else
     /home/linux/bin/ifcondition.sh $1
fi
使用root使用者來執行該指令碼試試。
[root@zeng ~]# /home/linux/bin/test_linux.sh ~./bash_history
You must be the linux user to run this script
[root@zeng ~]# echo $?
1
這裡的1 是我們設定的錯誤退出狀態。
巢狀if 語句
如果你覺得if 語句還不夠強大,那麼可以使用巢狀if 語句來實現更強大的功能。
下面使用巢狀if 語句來實現多分支。
[linux@zeng bin]$ cat best.sh
#!/bin/bash
#
#This shell script. is a demo for nested if statement.
#

os=0
echo "Which operating system you think is best?"
echo "1.Uinx operating system like Solaris,AIX..."
echo "2.Linux operating system like Red Hat linux,Suse linux..."
echo "3.Other,like Windows,MAC os..."
echo -n "Please select [1,2 or 3]?"
read os
if [ $os -eq 1 ]
then
echo "You pick up Unix."
else
    if [ $os -eq 2 ]
    then
    echo "You pick up linux."
    else 
        if [ $os -eq 3 ]
        then
        echo "You pick up other."
        else
        echo "Why you don't make a choice?"
        fi
    fi
fi
接下來我們測試這個指令碼。
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?1
You pick up Unix.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?2
You pick up linux.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?3
You pick up other.
[linux@zeng bin]$ best.sh
Which operating system you think is best?
1.Uinx operating system like Solaris,AIX...
2.Linux operating system like Red Hat linux,Suse linux...
3.Other,like Windows,MAC os...
Please select [1,2 or 3]?5
Why you don't make a choice?
多級if 語句
bash shell 為我們提供了靈活多樣的if 語句,來幫我們實現我們想要的功能。
多級if 語句的語法格式是:
if condition
           then
                       condition is zero (true - 0)
                       execute all commands up to elif statement
           elif condition1 
           then
                       condition1 is zero (true - 0)
                       execute all commands up to elif statement  
           elif condition2
           then
                       condition2 is zero (true - 0)
                       execute all commands up to elif statement          
           else
                       None of the above condtion,condtion1,condtion2 are true (i.e. 
                       all of the above nonzero or false)
                       execute all commands up to fi
           fi
下面這個shell script. 用來演示 多級if 語句。
[linux@zeng bin]$ cat multilevelif.sh
#!/bin/bash
#
#This shell script. test if...elif...else statement.
#
read -p "Enter a number:" n
if [ $n -gt 0 ]
  then
  echo "$n is a positive."
elif [ $n -lt 0 ]
  then
  echo "$n is a negative."
elif [ $n -eq 0 ]
  then
  echo "$n is zero number."
else
  echo "Oops! $n is not a number."
fi
[linux@zeng bin]$ multilevelif.sh
Enter a number:10
10 is a positive.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26110315/viewspace-720743/,如需轉載,請註明出處,否則將追究法律責任。

相關文章