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
Bash scripting unexpected operator
提问by Sonique
I'm writing script for git hook and have trouble with if
statement inside while
.
我正在为 git hook 编写脚本并且if
在while
.
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 if
doesn'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" ]
。
bash
accepts ==
instead of =
, but your /bin/sh
probably isn't bash.
bash
接受==
而不是=
,但你/bin/sh
可能不是bash。
So either change the ==
to =
, or your shebang to #!/bin/bash
因此,要么将==
to更改为=
,要么将您的 shebang更改为#!/bin/bash