linux shell編程if語句

shell 編程中使用到得if語句內判斷參數 shell

–b 當file存在而且是塊文件時返回真 編程

-c 當file存在而且是字符文件時返回真 bash

-d 當pathname存在而且是一個目錄時返回真 ide

-e 當pathname指定的文件或目錄存在時返回真 ui

-f 當file存在而且是正規文件時返回真 idea

-g 當由pathname指定的文件或目錄存在而且設置了SGID位時返回爲真 命令行

-h 當file存在而且是符號連接文件時返回真,該選項在一些老系統上無效 three

-k 當由pathname指定的文件或目錄存在而且設置了「粘滯」位時返回真 進程

-p 當file存在而且是命令管道時返回爲真 ip

-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用來檢查腳本的執行狀況。

相關文章
相關標籤/搜索