意外标记附近的语法错误 - bash

发布于 2024-12-13 14:16:57 字数 523 浏览 1 评论 0原文

我正在尝试编写一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装迷糊 2024-12-20 14:16:57
[ "${MODE}" == "top10gainers" ] || [ "${MODE}" == "top10losers" ]

或者

[ "${MODE}" == "top10gainers" -o "${MODE}" == "top10losers" ]
[ "${MODE}" == "top10gainers" ] || [ "${MODE}" == "top10losers" ]

or

[ "${MODE}" == "top10gainers" -o "${MODE}" == "top10losers" ]
顾铮苏瑾 2024-12-20 14:16:57

括号在子 shell 中执行其内容,这不是您想要的,并且字符串与单个 = 进行比较。

if [ "${MODE}" = "top10gainers" ] || [ "${MODE}" = "top10losers" ]; then
  echo "Top Gainers"
elif [ "${MODE}" = "solo" ]; then
     echo "Going solo"
fi

请注意,如果您以后想要比较数字,请使用 -eq 而不是 =

编辑:测试,我发现 == 也有效,但手册页中没有提及它们;它可能是 bash 内置版本的扩展。如果你想完全便携,我会坚持使用 =

Parentheses execute their contents in a subshell, that's not what you want, and strings are compared with a single =.

if [ "${MODE}" = "top10gainers" ] || [ "${MODE}" = "top10losers" ]; then
  echo "Top Gainers"
elif [ "${MODE}" = "solo" ]; then
     echo "Going solo"
fi

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.

扮仙女 2024-12-20 14:16:57

如果您显式使用 bash(即以 #!/bin/bash 而不是 #!/bin/sh 开头),另一种选择是使用 bash 的内置 < code>[[ 命令,如下所示:

if [[ ${MODE} == top10gainers || ${MODE} == top10loser ]]; then
    echo "Top Gainers"
elif [[ ${MODE} == solo ]]; then
    echo "Going solo"
fi

请注意,[[ 不需要引用可能包含空格的变量,这与 [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:

if [[ ${MODE} == top10gainers || ${MODE} == top10loser ]]; then
    echo "Top Gainers"
elif [[ ${MODE} == solo ]]; then
    echo "Going solo"
fi

Note that [[ doesn't require quoting around variables that may contain spaces, unlike [ or test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文