2013-11-22 190 views
0

我寫了一個函數來將yes或no的答案轉換爲true或false(0或1)。但是,每次運行我的腳本時,都會收到'command not found'錯誤。請幫我解決它測試返回值:未找到命令

get_boolean(){ 
    #============================================================================================== 
    # Returns false if the first argument is NO and returns true if it is YES. 
    # If the first argument is not a valid YES or NO, 
    # then the return value depends on the default specified by argument 2 (Default value) 
    #============================================================================================== 

    if [ "$1" == 'NO' ] || [ "$1" == 'no' ] || [ "$1" == 'No' ] || [ "$1" == 'N' ] || [ "$1" == 'n' ]; then 
      return 1; 
    elif [ "$1" == 'YES' ] || [ "$1" == 'yes' ] || [ "$1" == 'Yes' ] || [ "$1" == 'Y' ] || [ "$1" == 'y' ]; then 
      return 0; 
    elif [ "$2" == 'NO' ] || [ "$2" == 'no' ] || [ "$2" == 'No' ] || [ "$2" == 'N' ] || [ "$2" == 'n' ]; then 
      return 1; 
    elif [ "$2" == 'YES' ] || [ "$2" == 'yes' ] || [ "$2" == 'Yes' ] || [ "$2" == 'Y' ] || [ "$2" == 'y' ]; then 
      return 0; 
    fi 
} 

read -p 'Do you want to drop the table of invalids? [n]:' DROP_TABLE_OF_INVALIDS 
echo "After read: $DROP_TABLE_OF_INVALIDS" 
DROP_TABLE_OF_INVALIDS=get_boolean "$DROP_TABLE_OF_INVALIDS" 'n' 
echo "After assignment: $DROP_TABLE_OF_INVALIDS" 
if $DROP_TABLE_OF_INVALIDS; then 
     echo "Hello. I will drop the table" 
fi 

當我運行該腳本,我得到這些錯誤:

bash-3.2$ sh test.sh 
Do you want to drop the table of invalids? [n]:y 
After read: y 
test.sh: line 24: y: command not found 
After assignment: y 
test.sh: line 26: y: command not found 
bash-3.2$ sh test.sh 
Do you want to drop the table of invalids? [n]:n 
After read: n 
test.sh: line 24: n: command not found 
After assignment: n 
test.sh: line 26: n: command not found 

更新:(!感謝Barmar)下面的代碼工作

get_boolean(){ 
     #============================================================================================== 
     # Outputs false if the first argument is NO and outputs true if it is YES. 
     # If the first argument is not a valid YES or NO, 
     # then the output value depends on the default specified by argument 2 (Default value) 
     #============================================================================================== 

     if [ "$1" == 'NO' ] || [ "$1" == 'no' ] || [ "$1" == 'No' ] || [ "$1" == 'N' ] || [ "$1" == 'n' ]; then 
         echo false; 
     elif [ "$1" == 'YES' ] || [ "$1" == 'yes' ] || [ "$1" == 'Yes' ] || [ "$1" == 'Y' ] || [ "$1" == 'y' ]; then 
         echo true; 
     elif [ "$2" == 'NO' ] || [ "$2" == 'no' ] || [ "$2" == 'No' ] || [ "$2" == 'N' ] || [ "$2" == 'n' ]; then 
         echo false; 
     elif [ "$2" == 'YES' ] || [ "$2" == 'yes' ] || [ "$2" == 'Yes' ] || [ "$2" == 'Y' ] || [ "$2" == 'y' ]; then 
         echo true; 
     fi 
} 

read -p 'Do you want to drop the table of invalids? [n]:' DROP_TABLE_OF_INVALIDS 
echo "After read: $DROP_TABLE_OF_INVALIDS" 
DROP_TABLE_OF_INVALIDS=$(get_boolean "$DROP_TABLE_OF_INVALIDS" 'n') 
echo "After assignment: $DROP_TABLE_OF_INVALIDS" 
if $DROP_TABLE_OF_INVALIDS; then 
       echo "Hello. I will drop the table" 
fi 

這裏有編輯,使其工作:

  • 功能ñ'回聲'而不是返回。
  • 輸出值是bash true或false(用於在if中測試,否則將得到0:未找到命令或未找到1:命令)
  • 函數調用包含在'$('和')'

回答

2

的語法:

DROP_TABLE_OF_INVALIDS=get_boolean "$DROP_TABLE_OF_INVALIDS" 'n' 

意味着在執行命令` 「$ DROP_TABLE_OF_INVALIDS」 環境變量DROP_TABLE_OF_INVALIDS到設置爲字符串"get_boolean" 'N'」

給一個變量分配一個函數的輸出方式是:

DROP_TABLE_OF_INVALIDS=$(get_boolean "$DROP_TABLE_OF_INVALIDS" 'n') 

此外,您需要更改功能使用echo,而不是returnreturn設置退出狀態,而不是函數的輸出。

+0

謝謝!你很清楚,返回只是設置退出狀態,它不像C,C++,Java那樣是函數輸出。無論如何,我會發布修改代碼的修改。 – Shivanand

+0

我不確定你的'if $ DROP_TABLE_OF_INVALIDS'將在函數返回''「'時起作用。 '如果'需要一個論點。 – Barmar

+0

是的..我應該刪除它..我已經有一個默認值被傳遞給函數..再次感謝! – Shivanand

1

有幾個問題。該行

DROP_TABLE_OF_INVALIDS=get_boolean "$DROP_TABLE_OF_INVALIDS" 'n' 

不叫get_boolean。它會嘗試運行帶有修改後環境的由DROP_TABLE_OF_INVALIDS命名的命令。你會想:

DROP_TABLE_OF_INVALIDS=$(get_boolean "$DROP_TABLE_OF_INVALIDS" 'n') 

這導致了第二個問題,這是DROP_TABLE_OF_INVALIDS包含get_boolean標準輸出,但您使用的返回值。試試像這樣調用它:

if get_boolean "$DROP_TABLE_OF_INVALIDS" 'n'; then 
    ... 
fi 

其中返回值是測試目錄,而不是捕獲一個字符串來測試。

第三個問題是您嘗試返回空字符串。 return語句只能返回一個數值;它不像其他語言中返回任意值的函數。如果第一個參數爲空,則需要確定是真是假; false似乎是一個很好的默認值,所以返回0.或者,您可以忽略它將其視爲使用$2的值的情況。


一個簡單的版本將是

get_boolean(){ 
    #================================================================================== 
    # Returns false if the first argument is NO and returns true if it is YES. 
    # If the first argument is not a valid YES or NO, 
    # then the return value depends on the default specified by argument 2 (Default value) 
    #================================================================================== 
    case $1 in 
     NO|No|no|N|n|"") return 1 ;; 
     YES|yes|Yes|Y|y) return 0;; 
     *) case $2 in 
      YES|yes|Yes|Y|y) return 0;; 
      *) return 1 ;; 
      esac ;; 
    esac 
}