【问题标题】:Testing the return value : command not found测试返回值:找不到命令
【发布时间】:2013-12-07 15:57:26
【问题描述】:

我编写了一个函数来将是或否的答案转换为真或假(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:未找到命令)
  • 函数调用包含在'$('和')'中

【问题讨论】:

    标签: linux bash function boolean return-value


    【解决方案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
    }
    

    【讨论】:

      【解决方案2】:

      语法:

      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 设置退出状态,而不是函数的输出。

      【讨论】:

      • 谢谢!请您明确说明,return 只是设置退出状态,而不是与 C、C++、Java 不同的函数输出。无论如何,我将发布更正代码的编辑。
      • 我不确定当函数返回 "" 时您的 if $DROP_TABLE_OF_INVALIDS 是否有效。 if 需要一个参数。
      • 是的..我应该删除它..我已经有一个默认值被传递给函数..再次感谢!
      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 2011-02-05
      相关资源
      最近更新 更多