shell指令碼的結構化條件判斷命令

2021-10-02 07:28:44 字數 3567 閱讀 9927

程式可以說就是結構化加上演算法,現在就來講講shell指令碼的結構化命令

結構化命令允許你改變shell指令碼的正常執行流。最基本的結構化命令是if-then語句。也可以擴充套件if-then語句,加入if-then-else語句。如果希望在測試失敗時加上額外的測試,if-then-elif語句。

if command

then

command

fiif command

then

commands

else

commands

fiif command1

then

commands

elif command2

then

more commands

fi

#!/bin/bash

#測試條件結構化語句,檢視是否有某個使用者,如果有,則看

#是否有這個使用者的主目錄

testuser=nosuchuser

ifgrep

$testuser /etc/passwd

then

echo

"在系統裡有這個使用者"

else

echo

"在系統裡沒有這個使用者"

ifls -d /home/$testuser/

then

echo

"然而,$testuser目錄還在."

fifi

在很多指令碼中,你可能希望測試一種條件而不是乙個命令,比如數值、字串內容。test命令為你提供了測試這些條件的簡單方法。如果條件為true,test命令會為if-then語句產生退出狀態碼0.如果條件為false,test命令會為if-then語句產生乙個非零的退出狀態碼。

test命令

test condition

if test condition

then

commands

fi

#!/bin/bash

my_var=

"full"

iftest

$my_var

then

echo

"the expression return a true"

else

echo

"the expression return da false"

fi

如果test成功,返回0,如果失敗返回非零,執行else語句快

很少看到使用test命令的時候,大部分看的是方括號,方括號是與test命令同義。注意第乙個方括號之後和第二個方括號之前必須加上乙個空格。

if [ condition ]

then

commands

fi

#!/bin/bash

value1=10

value2=11if[

$value1 -gt 5 ]

then

echo

"測試value1的值大於5"

fiif

[$value1 -eq $value2

]then

echo

"值相等"

else

echo

"值不相等"

fi

執行的結果

miaodemacbook-air:shell miaochen$ ./test7.sh

測試value1的值大於5

值不相等

字串的比較

#!/bin/bash

testuser=richif[

$user

=$testuser

]then

echo

"welcome $testuser"

else

echo

"不是這個使用者"

fi

字串的大於號、小於號是認為是重定向符號,需要用到轉義。

#!/bin/bash

#比較字串的大小

var1=baseball

var2=hockyif[

$var1 \>

$var2

]then

echo

"var1大於var2"

else

echo

"var1小於var2"

fi

-n和-z可以檢查乙個變數是否含有資料

還有一類是檔案和目錄的比較,這可能是shell中用的最多,最為強大的比較形式

-d file 檢查file是否是乙個目錄

-e file 檢查file是否存在

-f file 檢查file是否存在並是乙個檔案

-r file 檢查file是否存在並可讀

-s file 檢查file是否存在並非空

-w file. 檢查檔案是否存在並可寫

-x file 檢查檔案是否存在並可執行

-o file 檢查檔案是否存在並屬於當前使用者所有

-g file 檢查檔案file是否存在並且預設組與當前使用者相同

file1 -nt file2 檢查檔案file1是否比file2新

file1 -ot file2. 檢查檔案file1是否比file2舊

符合條件測試

[ condition ] && [ condition ]

[ condition ] || [ condition ]

雙括號使用另一種操作符進行高階數學計算。雙方括號命令允許高階字串模式匹配運算。

(( expression ))

expression可以是任意的數字賦值或比較表示式。

#!/bin/bash

var1=10

if(( $var1 **

2>

90))

then

(( var2 = $var1 **2))

echo

"var1的平方是$var2"

fi

雙方括號的例子

[[ expression ]]

expression模式匹配,比如正規表示式

case命令,該命令是執行多個if-then-else命令的簡便形式,類似c語言中的switch() case

#!/bin/bash

username=

$(echo $logname)

case

$username

inmiaochen)

echo

"welcome,$username";;

rich)

echo

"rich 的賬號";;

testing)

echo

"這是乙個測試賬號";;

*)echo

"星號會捕獲與已知模式不匹配的值";;

esac

shell結構化語句,判斷,迴圈

命令替換 testing date testing2 date 數字運算 test3 1 5 只支援整數運算var1 10.46 var2 43.67 var3 33.2 var4 71 var5 bc 1.txt scale 4 a1 var1 var2 b1 var3 var4 a1 b1 eo...

shell指令碼結構化之迴圈命令

迴圈是程式設計的乙個重要部分,bash shell提供了三種可用於指令碼中的循壞命令 for 命令 while命令 until 命令 這些都沒有好講的,注意下格式就行了,看兩個例項 bin bash ifs for folder in path doecho folder forfile in fo...

結構化程式設計 多支條件判斷switch case

有些條件判斷並非簡單的真假模式,而是在多種可能中選擇乙個處理。這種情況,我們需要使用switch case語句進行處理。以下是對上海地鐵自動售票機的模擬 code static void main string args 元票價 3 break case 4 system.console.write...