shell 比較邏輯表示式

2021-05-23 13:49:41 字數 2153 閱讀 7115

[ -a file ] 如果 file 存在則為真。

[ -b file ] 如果 file 存在且是乙個塊特殊檔案則為真。

[ -c file ] 如果 file 存在且是乙個字特殊檔案則為真。

[ -d file ] 如果 file 存在且是乙個目錄則為真。

[ -e file ] 如果 file 存在則為真。

[ -f file ] 如果 file 存在且是乙個普通檔案則為真。

[ -g file ] 如果 file 存在且已經設定了sgid則為真。

[ -h file ] 如果 file 存在且是乙個符號連線則為真。

[ -k file ] 如果 file 存在且已經設定了粘制位則為真。

[ -p file ] 如果 file 存在且是乙個名字管道(f如果o)則為真。

[ -r file ] 如果 file 存在且是可讀的則為真。

[ -s file ] 如果 file 存在且大小不為0則為真。

[ -t fd ] 如果檔案描述符 fd 開啟且指向乙個終端則為真。

[ -u file ] 如果 file 存在且設定了suid (set user id)則為真。

[ -w file ] 如果 file 如果 file 存在且是可寫的則為真。

[ -x file ] 如果 file 存在且是可執行的則為真。

[ -o file ] 如果 file 存在且屬有效使用者id則為真。

[ -g file ] 如果 file 存在且屬有效使用者組則為真。

[ -l file ] 如果 file 存在且是乙個符號連線則為真。

[ -n file ] 如果 file 存在 and has been mod如果ied since it was last read則為真。

[ -s file ] 如果 file 存在且是乙個套接字則為真。

[ file1 -nt file2 ] 如果 file1 has been changed more recently than file2, or 如果 file1 exists and file2 does not則為真。

[ file1 -ot file2 ] 如果 file1 比 file2 要老, 或者 file2 存在且 file1 不存在則為真。

[ file1 -ef file2 ] 如果 file1 和 file2 指向相同的裝置和節點號則為真。

[ -o optionname ] 如果 shell選項 「optionname」 開啟則為真。

[ -z string ] 「string」 的長度為零則為真。

[ -n string ] or [ string ] 「string」 的長度為非零 non-zero則為真。

[ string1 == string2 ] 如果2個字串相同。 「=」 may be used instead of 「==」 for strict posix compliance則為真。

[ string1 != string2 ] 如果字串不相等則為真。

[ string1 < string2 ] 如果 「string1」 sorts before 「string2」 lexicographically in the current locale則為真。

[ string1 > string2 ] 如果 「string1」 sorts after 「string2」 lexicographically in the current locale則為真。

[ arg1 -eq arg2 ] 等於

[ arg1 -ne arg2 ] 不等於

[ arg1 -gt arg2 ] 大於

[ arg1 -lt arg2 ] 小於

[ arg1 -le arg2 ] 小於等於

[ arg1 -ge arg2 ] 大於等於

if [ $i -le 10 ] ; then #第乙個(中括號)[和if及$i之間都必須有空格,10和第二個(中括號)]之間必須有空格,但是(中括號)]和;之間的空格可有可無

echo 0$i

else

echo $i

ficase $i in

1|3|5|7|9|11|13|15) echo 00000;; #後面必須是兩個分號;;

2|4|6|8|10|12|14) echo 11111;; #後面必須是兩個分號;;

esac

shell 邏輯表示式彙總(if,大小比較)

比較字串 判斷檔案是否存在及是否可讀等,通常用 來表示條件測試 注意 這裡的空格很重要。要確保方括號的空格 空格缺少或位置不對,if then elif then else fi命令解釋 f somefile 判斷是否是乙個檔案 x bin ls 判斷 bin ls是否存在並有可執行許可權 n va...

shell 四 邏輯表示式

一邏輯操作符 閘電路 說明 學習中,使用中記憶!and 乘法,or是加法練習 root server1 mnt ll total 8 1 root root 11 aug 29 12 29 temp drwxr xr x 4 root root 34 aug 29 12 12 wzj rw r r ...

邏輯表示式

1.比較表示式只能描述1個條件 2.邏輯表示式可以描述多個條件,並且使用邏輯運算子來指定多個條件之間的關係 符號含義 與 ll或 非 邏輯與表示式 邏輯與 由 連線起來的邏輯表示式,只要當兩邊的條件都成立的時候,整個邏輯表示式猜成立,只要有一邊的條件不成立,那麼整個邏輯表示式都不成立 表示式結果 1...