2012-11-22 68 views
3

我想測試兩個變量是否存在,之後我使用「read」創建它。如果用戶只輸入我想要的兩個變量中的一個,則會顯示錯誤。bash中的錯誤 - 一元運算符預期存在-e

有我的代碼:

while true; 
do 
    echo "Saisissez deux variables x et y sous la forme [x y]" 
    read x y 

    if [ !-e $x ] || [ !-e $y ] <<<<<< problem ligne 
    then 
    echo "Vous devez renseigner deux nombres x et y" 
    elif [ $x = "." ] 
    then 
    exit 0 
    else 
    calcul $x $y 
    fi 
done 

並沒有當我剛進入一個參數錯誤:

[: !-e: unary operator expected 

感謝您的幫助:)

+1

如果你把什麼空間了''之間和'-e '? –

+1

'-e'運算符檢查是否存在具有該名稱的文件。 '!後面需要一個空格!' - shell要求每個命令和參數都是一個單獨的標記(除括號,重定向和其他一些語法細節外)。你也應該大致雙引號的用戶輸入,所以' 「$ X」''親$ x'和' 「$ Y」''親$ y'。 – tripleee

+0

感謝您的回覆:)我試圖檢測用戶是否按下了沒有任何價值的回車鍵。我試圖用-e來防止這一點,但它不工作:/ – toshiro92

回答

7

將其更改爲:

if [ -z "$x" ] || [ -z "$y" ] 

說明

  • [實際上是一個shell內置(嘗試which [help [您提示);它是test的同義詞。
  • -z[的一個參數。它的意思是「測試如果下一個字符串的長度爲0;返回true,如果是這樣;返回false,否則
  • 始終包你用雙引號測試變量

這裏是有用的選名單! [,因爲我認爲你會感興趣:

-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
file1 –nt file2 = True if file1 is newer, by modification date, than file2. 
file1 ot file2 = True if file1 is older than file2. 
file1 ef file2 = True if file1 and file2 have the same device and inode numbers. 
-z string = True if the length of the string is 0. 
-n string = True if the length of the string is non-zero. 
string1 = string2 = True if the strings are equal. 
string1 != string2 = True if the strings are not equal. 
!expr = True if the expr evaluates to false. 
expr1 –a expr2 = True if both expr1 and expr2 are true. 
expr1 –o expr2 = True is either expr1 or expr2 is true. 
+0

是的:)在bash:如果您要檢查它們實際上的數字,也許像「總是給當你正在測試變量」 –

+0

''$'in''| * [!0-9] *)用法語死亡;; esac'和同上爲'$ y'。 – tripleee

+0

感謝補:)(死在法國 - >「mourir」) 和它的作品,但如果我只是按ENTER鍵,沒有任何價值,這是行不通的。我試圖用-e來防止這一點,但它不工作:/ – toshiro92

1

運營商-e是不是在這種情況下,使用權操作員操作-z是右操作它檢查是否一個字符串是空的;在。你的情況xy

所以更改此設置:

if [ !-e $x ] || [ !-e $y ] 

這樣:

if [ ! -z $x ] || [ ! -z $y ] 

操作-e是用來檢查文件是否存在。

+0

呀它的工作原理,但如果我只是按ENTER鍵,沒有任何價值,這是行不通的。我試圖用-e來防止這種情況,但它不起作用:/ – toshiro92

0

在bash中,你可以使用:

if [[ -z $x || -z $y ]]; then 

[[具有多種功能,這使得它更容易使用比test/[使用,其中包括把||&&直接進入表達的能力,和事實上你不需要引用參數擴展,因爲[[會自動執行。

help [[ 

會給你一些更多的信息,但它忽略了這個有用的段落,你可以在man bash發現:

Word splitting and pathname expansion are not performed on the 
    words between the [[ and ]]; tilde expansion, parameter and 
    variable expansion, arithmetic expansion, command substitution, 
    process substitution, and quote removal are performed. Conditional 
    operators such as -f must be unquoted to be recognized as primaries. 
相關問題