Bash 脚本意外操作符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24464690/
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-18 10:45:18  来源:igfitidea点击:

Bash scripting unexpected operator

bashshell

提问by Sonique

I'm writing script for git hook and have trouble with ifstatement inside while.

我正在为 git hook 编写脚本并且ifwhile.

File:

文件:

#!/bin/sh
while read oldrev newref ref
do
    branch=$(git rev-parse --symbolic --abbrev-ref $ref)

    if [ "a"  == "a" ]
    then
        echo "Condition work"
    fi

    echo "$branch"
done

Error:

错误:

hooks/post-receive: 6: [: a: unexpected operator

I'll try with variables, double quotes but ifdoesn't work. What kind of error is here?

我会尝试使用变量,双引号,但if不起作用。这里有什么样的错误?

Thanks

谢谢

回答by mata

if [ "a" == "a" ]should be if [ "a" = "a" ].

if [ "a" == "a" ]应该是if [ "a" = "a" ]

bashaccepts ==instead of =, but your /bin/shprobably isn't bash.

bash接受==而不是=,但你/bin/sh可能不是bash。

So either change the ==to =, or your shebang to #!/bin/bash

因此,要么将==to更改为=,要么将您的 shebang更改为#!/bin/bash