我寫了一個函數來將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:命令)
- 函數調用包含在'$('和')'
謝謝!你很清楚,返回只是設置退出狀態,它不像C,C++,Java那樣是函數輸出。無論如何,我會發布修改代碼的修改。 – Shivanand
我不確定你的'if $ DROP_TABLE_OF_INVALIDS'將在函數返回''「'時起作用。 '如果'需要一個論點。 – Barmar
是的..我應該刪除它..我已經有一個默認值被傳遞給函數..再次感謝! – Shivanand