linux - 测试返回值 : command not found

标签 linux bash function boolean return-value

我编写了一个函数来将是或否的答案转换为真或假(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 而不是 returnreturn 设置退出状态,而不是函数的输出。

关于linux - 测试返回值 : command not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20150478/

相关文章:

python - 函数定义 : Matching Area Codes to Phone Numbers

c++ - Boost:bind 和 Boost::function

linux - Bash 一种在出现管道错误时保留原始管道输出文件的方法?

python - 通过 pyodbc 和 Linux(使用 Docker)访问 i Series AS400 数据库时出现问题

linux - 来自现有类的 C++11 线程

linux - Crontab 脚本不生成文本文件

mysql - bash 脚本中的循环未捕获文件中的所有行

bash - Powershell 问题 - 在 Git Bash 中执行命令

c - 将多维数组传递给 C 中的函数

linux - 用于检查进程是否正在运行的 Shell 脚本