shell中的條件判斷

來源:互聯網
上載者:User

標籤:

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中的條件判斷

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.