bash 的判斷檔案操作語句

2021-07-23 21:57:06 字數 2531 閱讀 7999

unix shell 程式設計(1)

-e file file存在

-f file file存在並且是普通檔案

-r file file有讀許可權

-s file file存在且不為空

-w file file寫許可權

-x file file有執行許可權

-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 如果 file1file2 does not則為真。

exists and [ 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 op arg2 ] 「op」 is one of -eq, -ne, -lt, -le, -gt or -ge. these arithmetic binary operators return true if 「arg1」 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to 「arg2」, respectively. 「arg1」 and 「arg2」 are integers. 表示式可以藉以下操作符組合起來,以降序列出:listed in decreasing order of precedence: 表 7.2. 組合表示式操作 效果

[ ! expr ] 如果 expr 是false則為真。

[ ( expr ) ] 返回 expr的值。這樣可以用來忽略正常的操作符優先順序。

[ expr1 -a expr2 ] 如果 expr1 and expr2 全真則為真。

-eq 等於

-ne 不等於

-gt 大於

-lt 小於

-le 小於等於

-ge 大於等於

-z 空串

* = 兩個字元相等

* != 兩個字元不等

* -n 非空串

BASH系列之四比較和判斷語句

分類 linux作業系統 2011 08 13 16 35 25人閱讀收藏 舉報 shell中的比較和判斷 一 比較 操作符比較操作 整數操作 字串操作 相同 eq 不同 nq 大於 gt 小於 lt 大於等於 ge 小於等於 le 為空 z 不為空 n 範例 1 比較整數a和b是否相等 if a ...

bash的if語句注意點

if commands then commands elif commands then commands.else commands.else commands fi這裡if後面的是否為true,和c語言中非零就是true正好相反,它代表測試command是否執行成功。而一般command執行成功...

linux shell 判斷檔案操作

shell判斷資料夾是否存在 如果資料夾不存在,建立資料夾 if d myfolder then mkdir myfolder fi shell判斷檔案,目錄是否存在或者具有許可權 folder var www file var www log x 引數判斷 folder 是否存在並且是否具有可執行...