標籤:
read命令 命令的文法:
read [參數] 變數名 常用的參數如下:
-t timeout 設定逾時時間。
-p prompt 設定提示資訊,該提示資訊將會顯在游標前。
條件測試:
條件測試可以判斷指令碼程式啟動並執行狀態。它常用於條件分支,以便在不同的運行狀態下執行不同的命令。
[ param1 判斷符號 param2 ]
[ 判斷符號 param ]
檔案狀態測試:
-b filename 判斷檔案filename是否為塊裝置,若檔案不存在返回假。
-c filename 判斷檔案filename是否為字元裝置,若檔案不存在返回假。
-d pathname 判斷路徑pathname是否為目錄,若路徑不存在返回假。
-e pathname 判斷路徑pathname是否存在。
-f filename 判斷檔案filename是否為真會檔案,若檔案不存在返回假。
-g pathname 判斷路徑pathname是否設定了SGID位,若路徑不存在返回假。
-h filename 判斷檔案filename是否為連結檔案,若檔案不存在返回假。
-k pathname 判斷路徑pathname是否設定了“粘滯”位,若路徑不存在返回假。
-p filename 判斷檔案filename是否為管道檔案,若檔案不存在返回假。
-r pathname 判斷路徑pathname是否為可讀,若路徑不存在返回假。
-s filename 判斷檔案filename的尺寸是否大於0,若檔案不存在返回假。
-S filename 判斷檔案filename是否是Socket,檔案不存在返回假。
-u pathname 判斷路徑pathname是否設定了SUID位,若路徑不存在返回假。
-w pathname 判斷路徑pathname是否為可寫,若路徑不存在返回假。
-x pathname 判斷路徑pathname是否為可執行,若路徑不存在返回假。
-O pathname 判斷路徑pathname是否為目前使用者所擁有,若路徑不存在返回假。
-G pathname 判斷路徑pathname的使用者組是否為目前使用者所在組,若路徑不存在返回假。
file1 -nt file2 判斷檔案file1是否比檔案file2新。
file1 -ot file2 判斷檔案file1是否比檔案file2舊。
邏輯操作
param1 –a param2 邏輯與。
param1 –o param2 邏輯或。
! param 邏輯非。
字元測試:
-z string 判斷字串string是否為空白串,即長度為0。
-n string 判斷字串string 是否為非空串。
string1 = string2 判斷字串string1和string2是否相等。
string1 != string2 判斷字串string1和string2是否不等。
string1 < string2 按字元編碼表排序,字串string1是否在string2之前。
string1 > string2 按字元編碼表排序,字串string1是否在string2之後。
數值測試:
val1 –eq val2 判斷val1和val2的數值是否相等。
val1 –ne val2 判斷val1和val2的數值是否不等。
val1 –lt val2 判斷val1是否小於val2。
val1 –le val2 判斷val1是否小於等於val2。
val1 –gt val2 判斷val1是否大於val2。
val1 –ge val2 判斷val1是否大於等於val2。
shell中的條件判斷