我编写了一个函数来将是或否的答案转换为真或假(0 或 1)。但是,每次运行我的脚本时,我都会收到“找不到命令”错误。请帮我解决问题
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:未找到命令)
- 函数调用包含在“$(”和“)”中
最佳答案
语法:
DROP_TABLE_OF_INVALIDS=get_boolean "$DROP_TABLE_OF_INVALIDS" 'n'
表示在执行命令`"$DROP_TABLE_OF_INVALIDS"'n'"时设置环境变量DROP_TABLE_OF_INVALIDS
为字符串"get_boolean"
。
用函数的输出赋值给变量的方法是:
DROP_TABLE_OF_INVALIDS=$(get_boolean "$DROP_TABLE_OF_INVALIDS" 'n')
此外,您需要更改函数以使用 echo
而不是 return
。 return
设置退出状态,而不是函数的输出。
关于linux - 测试返回值 : command not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20150478/