shell判斷和比較

來源:互聯網
上載者:User

shell判斷和比較 1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second...$#        The number of command-line parameters.$0        The name of current program.$?        Last command or function's return value.$$        The program's PID.$!        Last program's PID.$@        Save all the parameters. almost any shell book will talk about them,from which you can get their detail usages. 2    Linux SHELL if 命令參數說明 –b 當file存在並且是塊檔案時返回真-c 當file存在並且是字元檔案時返回真-d 當pathname存在並且是一個目錄時返回真-e 當pathname指定的檔案或目錄存在時返回真-f 當file存在並且是正規檔案時返回真-g 當由pathname指定的檔案或目錄存在並且設定了SGID位時返回為真-h 當file存在並且是符號連結檔案時返回真,該選項在一些老系統上無效-k 當由pathname指定的檔案或目錄存在並且設定了“粘滯”位時返回真-p 當file存在並且是命令管道時返回為真-r 當由pathname指定的檔案或目錄存在並且可讀時返回為真-s 當file存在檔案大小大於0時返回真-u 當由pathname指定的檔案或目錄存在並且設定了SUID位時返回真-w 當由pathname指定的檔案或目錄存在並且可執行時返回真。一個目錄為了它的內容被訪問必然是可執行檔。-o 當由pathname指定的檔案或目錄存在並且被子當前進程的有效使用者ID所指定的使用者擁有時返回真。UNIX Shell 裡面比較字元寫法: -eq   等於-ne    不等於-gt    大於-lt    小於-le    小於等於-ge   大於等於-z    空串=     兩個字元相等!=    兩個字元不等-n    非空串總結: 文檔比較子  -e filename  假如 filename存在,則為真  [ -e /var/log/syslog ] -d filename  假如 filename為目錄,則為真  [ -d /tmp/mydir ] -f filename  假如 filename為常規文檔,則為真  [ -f /usr/bin/grep ] -L filename  假如 filename為符號連結,則為真  [ -L /usr/bin/grep ] -r filename  假如 filename可讀,則為真  [ -r /var/log/syslog ] -w filename  假如 filename可寫,則為真  [ -w /var/mytmp.txt ] -x filename  假如 filename可執行,則為真  [ -L /usr/bin/grep ] filename1-nt filename2  假如 filename1比 filename2新,則為真  [ /tmp/install/etc/services -nt /etc/services ] filename1-ot filename2  假如 filename1比 filename2舊,則為真  [ /boot/bzImage -ot arch/i386/boot/bzImage ] 字串比較運算子 (請注意引號的使用,這是防止空格擾亂代碼的好方法)  -z string  假如 string長度為零,則為真  [ -z "$myvar" ] -n string  假如 string長度非零,則為真  [ -n "$myvar" ] string1= string2  假如 string1和 string2相同,則為真  [ "$myvar" = "one two three" ] string1!= string2  假如 string1和 string2不同,則為真  [ "$myvar" != "one two three" ] 算術比較子  num1-eq num2  等於 [ 3 -eq $mynum ] num1-ne num2  不等於 [ 3 -ne $mynum ] num1-lt num2  小於 [ 3 -lt $mynum ] num1-le num2  小於或等於 [ 3 -le $mynum ] num1-gt num2  大於 [ 3 -gt $mynum ] num1-ge num2  大於或等於 [ 3 -ge $mynum ]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.