2014-10-19 85 views
0

它似乎可以作爲一個 反轉...
例如:unix-shell條件中的-w運算符是什麼?

#!/bin/bash 
set -x 
#a=1 
b=2 
if [ "$a" -o -w "$b" ]; then 
    echo "ok" 
else 
    echo "fail" 
fi 

返回 「失敗」。

,但我不知道,因爲

#!/bin/bash 
set -x 
#a=1 
#b=2 
if [ "$a" -o -w "$b" ]; then 
    echo "ok" 
else 
    echo "fail" 
fi 

也返回 「失敗」。如果替換-w!這返回「OK」。

請告訴我,這個操作符記錄在哪裏。 Bash和sh手冊僅包含-a和-o操作符。

+0

爲'test'的'bash'手冊頁項只提到'-a'和'-o'的名字,但它確實將您引用到條件表達式部分,其中記錄了其他的基本內容。順便說一句,'-a'和'-o'被認爲是過時的;你應該使用'[「$ a」] || [-w「$ b']' – chepner 2014-10-20 16:24:11

回答

2

它測試文件是否存在並且是可寫的。閱讀test命令的手冊。

+1

因爲'['實際上是一個命令,幾乎與'test'命令相同(不同之處在於'['require']'作爲它的最後一個參數) – 2014-10-19 19:21:18

+0

'test ''''''和'['是同樣的命令,它們實際上是在同一個二進制文件中的兩個鏈接 – 2014-10-19 19:23:14

+0

謝謝,我知道-w FILE,但我不知道可以從變量申請數值。我看到它是如何工作的 – Alexandr 2014-10-19 19:31:23

3

這是記錄在man bash,和相同的摘錄,可通過help test

-w FILE  True if the file is writable by you. 
0

只是一些額外的信息:

[ -e file ] : if "file" exists 
[ -f file ] : if "file" exists and it is a file 
[ -s file ] : if "file" exists and it is not empty 
[ -d file ] : to check if "file" exists and it is a directory 
[ -r file ] : if "file" exists and readable 
[ -w file ] : if "file" exists and writable 
[ -x file ] : if "file" exists and it is executable 
[ -O file ] : if "file" exists and it is owned by current user 
[ -G file ] : if "file" exists and default group same as the current user 
[ file_1 -nt file_2 ] : if "file_1" newer than "file_2" 
[ file_1 -ot file_2 ] : if "file_1" older than "file_2"