2014-02-10 44 views
0

我有以下bash腳本文件callee.sh從另一個腳本文件caller.sh中調用。完全混淆關於bash腳本命令行參數

的callee.sh如下:

if [ $1 -eq 1 ]; 
then  
    echo inside $1 
    source ~/MYPROGRAMSRC/standAloneWordCount.sh $2 
    #echo "inside standalone branch" 
    #echo $1 

elif [ $1 -eq 2 ]; 
then 
    #echo "inside distributed branch" 
    #echo $1 

else 
    echo invalid option for first argument-\n Options:\n "distributed"\n or\n "standalone"\n 


fi 

由於大多數人也許能告訴,這是我用它來決定是否在執行Hadoop腳本分佈式或獨立模式取決於參數。

此腳本從caller.sh如下

source callee.sh $2 $counterGlobal 

其中$ 2是一個數字是1或2,$ counterGlobal是某個整數調用。

我的問題是,在callee.sh中的if條件永遠不會計算爲True,因此我從callee.sh中調用的腳本standAloneWordCount.sh從不會被調用。我使用bash shell中運行,並嘗試了許多變種if語句,如:

if [ $(($1 == 1)) ] -- (1) 

在echo語句正上方的線 - (1),表達式$(($ 1 == 1))的計算結果到1,所以我很困惑,爲什麼我不能滿足條件。

而且我不斷收到錯誤,它說:

syntax error near unexpected token `else' 

如果有人可以幫助我走出這兩個誤區,這將是大加讚賞。因爲我已經沒有想法了。

在此先感謝!

回答

1

試圖像if語句的許多變種:

if [ $(($1 == 1)) ]

而應該是說:

if (($1 == 1)); then 
    ... 
fi 

關於Syntax error near unexpected token else'`,這不是因爲你上面顯示的任何代碼。它似乎來自腳本的其他部分。

+0

以及實際上原來還有別的東西錯了,因爲它一直在說每次我關閉並重新打開的出了毛病,那就是正在使用的.SWP文件的文件。所以我刪除了.swp文件,你的解決方案工作。謝謝。 – anonuser0428

0

如果你正在使用bash,請嘗試使用雙括號:

if [[ $1 -eq 1 ]]; then 
    echo "inside 1" 
fi 

至於syntax error,你需要在你的文字引號(這也意味着逃避現有的報價或使用單引號):

echo -e "invalid option for first argument-\n Options:\n \"distributed\"\n or\n \"standalone\"\n" 

-e標誌是有沒有讓慶典知道你想要的\n評估一個換行符。