意外标记附近的语法错误 - bash
我正在尝试编写一个简单的 bash 脚本。它只是根据满足某些条件输出几行。我陷入了 if-else 条件,似乎无法找到出路。
这是代码:
if [ ( "${MODE}" == "top10gainers" ) || ( "${MODE}" == "top10losers" ) ]; then
echo "Top Gainers"
elif [ "${MODE}" == "solo" ]; then
echo "Going solo"
fi
我得到的错误是:
syntax error near unexpected token `"${MODE}"'
`if [ ( "${MODE}" == "top10gainers" ) || ( "${MODE}" == "top10losers" ) ]; then'
我已经用谷歌搜索并尝试搜索论坛(包括SO)但没有找到解决方案。我还在 OR
条件下尝试了不同的括号,但它们也没有起作用。
I am trying to write a simple bash
script. It just puts out a few lines based on some conditions being satisfied. I am getting stuck on an if-else
condition and cannot seem to figure a way out.
Here is the code:
if [ ( "${MODE}" == "top10gainers" ) || ( "${MODE}" == "top10losers" ) ]; then
echo "Top Gainers"
elif [ "${MODE}" == "solo" ]; then
echo "Going solo"
fi
The error I get is:
syntax error near unexpected token `"${MODE}"'
`if [ ( "${MODE}" == "top10gainers" ) || ( "${MODE}" == "top10losers" ) ]; then'
I have googled and tried to search forums (including SO) but have not come across a solution. I have also tried out different brackets in the OR
condition, but they have not worked either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者
or
括号在子 shell 中执行其内容,这不是您想要的,并且字符串与单个
=
进行比较。请注意,如果您以后想要比较数字,请使用
-eq
而不是=
。编辑:测试,我发现
==
也有效,但手册页中没有提及它们;它可能是 bash 内置版本的扩展。如果你想完全便携,我会坚持使用=
。Parentheses execute their contents in a subshell, that's not what you want, and strings are compared with a single
=
.Note that if you want to compare numbers in the future, use
-eq
instead of=
.Edit: Testing, I found
==
works too, but there's no mention of them in the man page; it might be an extension in the bash built-in version. I'd stick to=
if you want to be at all portable.如果您显式使用 bash(即以
#!/bin/bash
而不是#!/bin/sh
开头),另一种选择是使用 bash 的内置 < code>[[ 命令,如下所示:请注意,
[[
不需要引用可能包含空格的变量,这与[
或test 不同
。Another option, if you are explicitly using bash (that is, starting with
#!/bin/bash
instead of#!/bin/sh
) is to use bash's builtin[[
command, like so:Note that
[[
doesn't require quoting around variables that may contain spaces, unlike[
ortest
.