linux shell if 參數

來源:互聯網
上載者:User

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 ]

  指令碼樣本:

  #!/bin/bash

  # This script prints a message about your weight if you give it your

  # weight in kilos and hight in centimeters.

  if [ ! $# == 2 ]; then

  echo "Usage: $0 weight_in_kilos length_in_centimeters"

  exit

  fi

  weight="$1"

  height="$2"

  idealweight=$[$height - 110]

  if [ $weight -le $idealweight ] ; then

  echo "You should eat a bit more fat."

  else

  echo "You should eat a bit more fruit."

  fi

  # weight.sh 70 150

  You should eat a bit more fruit.

  # weight.sh 70 150 33

  Usage: ./weight.sh weight_in_kilos length_in_centimeters

  位置參數 $1, $2,..., $N,$#代表了命令列的參數數量, $0代表了指令碼的名字,

  第一個參數代表$1,第二個參數代表$2,以此類推,參數數量的總數存在$#中,上面的例子顯示了怎麼改變指令碼,如果參數少於或者多餘2個來列印出一條訊息。

  執行,並查看情況。

  # bash -x tijian.sh 60 170

  + weight=60

  + height=170

  + idealweight=60

  + '[' 60 -le 60 ']'

  + echo 'You should eat a bit more fat.'

  You should eat a bit more fat.

  其中-x用來檢查指令碼的執行情況。

相關文章

聯繫我們

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