在 bash 脚本中遇到“预期的一元运算符”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:06:39  来源:igfitidea点击:

encounter "unary operator expected" in bash script

bashif-statementunary-operator

提问by G3Y

in my bash script, I have a function to return 0 or 1(true or false) for the later main function's condition.

在我的 bash 脚本中,我有一个函数可以为后面的主函数的条件返回 0 或 1(真或假)。

function1 () { 
    if [[ "" =~ "^ ...some regexp... $" ]] ; then
        return 1
    else
        return 0
    fi 
}

then in my main function:

然后在我的主要功能中:

main () { 
    for arg in ${@} ; do
        if [ function1 ${arg} ] ; then
            ...
        elif [ ... ] ; then
            ...
        fi
    done
}

however, when i ran this script it always gave me an error msg "[: function1: unary operator expected"

然而,当我运行这个脚本时,它总是给我一个错误消息“[:function1:一元运算符预期”

can anyone help me please?

有人可以帮我吗?

回答by tripleee

You are making the common mistake of assuming that [is part of the ifcommand's syntax. It is not; the syntax of ifis simply

您犯了一个常见的错误,认为这[if命令语法的一部分。它不是; 的语法if很简单

if command; then
    ... things which should happen if command's result code was 0
else
    ... things which should happen otherwise
fi

One of the common commands we use is [which is an alias for the command test. It is a simple command for comparing strings, numbers, and files. It accepts a fairly narrow combination of arguments, and tends to generate confusing and misleading error messages if you don't pass it the expected arguments. (Or rather, the error messages are adequate and helpful once you get used to it, but they are easily misunderstood if you're not used.)

command我们使用的常见s 之一是[which 是命令的别名test。这是一个用于比较字符串、数字和文件的简单命令。它接受相当狭窄的参数组合,并且如果您没有将预期的参数传递给它,则往往会产生令人困惑和误导性的错误消息。(或者更确切地说,一旦您习惯了错误消息就足够了并且很有帮助,但是如果您不使用它们,它们很容易被误解。)

In your mainfunction, the call to [appears misplaced. ?You probably mean

在您的main函数中,对 的调用[似乎放错了位置。?你可能是说

if function "$arg"; then
    ...
elif ... ; then ...

By the way, for good measure, you should also always quote your strings. Use "$1"not $1, and "$arg"instead of $arg.

顺便说一句,为了更好的衡量,您还应该始终引用您的字符串。使用"$1"not $1,而"$arg"不是$arg

The historical reasons for testas a general kitchen sink of stuff the authors didn't want to make part of the syntax of ifis one of the less attractive designs of the original Bourne shell. Bash and zshoffer alternatives which are less unwieldy (like the [[double brackets in bash, which you use in your function1definition), and of course, POSIX testis a lot more well-tempered than the original creation from Bell Labs.

test作者不想将其作为语法的一部分作为一般厨房水槽的历史原因if是原始 Bourne shell 不太吸引人的设计之一。Bash 并zsh提供不那么笨拙的替代方案(例如[[bash 中的双括号,您在function1定义中使用它),当然,POSIXtest比 Bell Labs 的原始创造要好得多。

As an additional clarification, your function can be simplified to just

作为额外的说明,您的功能可以简化为

function1 () {
    ! [[ "" =~ "^ ...some regexp... $" ]]
}

That is, perform the test with [[and reverse its result code. (The "normal" case would be to return 0 for success, but maybe you are attempting to verify that the string doesn't match?)

也就是说,使用[[并反转其结果代码执行测试。(“正常”情况是返回 0 表示成功,但也许您正在尝试验证字符串不匹配?)