测试返回值:找不到命令

时间:2013-11-22 17:06:02

标签: linux bash function boolean return-value

我编写了一个函数来将是或否的答案转换为true或false(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

以下是使其有效的编辑内容:

  • 功能' echo而不是返回。
  • 输出值为bash true或false(用于测试if,否则获取0:命令未找到或1:命令未找到)
  • 函数调用包含在' $('和')'

2 个答案:

答案 0 :(得分: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设置退出状态,而不是函数的输出。

答案 1 :(得分: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
}