bash Shell - 表达式中的语法错误(错误标记为“0”)

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

Shell - syntax error in expression (error token is "0 ")

bashshelltesting

提问by Augusto

What is wrong with this code?

这段代码有什么问题?

if  (( `date +%U` % 2 -eq 0 ))
then
   VOLUME= "A"
else
    VOLUME="B"
fi

I′m getting "syntax error in expression (error token is "0 ")" eror.

我收到“表达式中的语法错误(错误标记为“0”)”错误。

回答by anubhava

You need to use command substitution using $(...)syntax.

您需要使用$(...)语法来使用命令替换。

You can use this command:

你可以使用这个命令:

(( $(date +%U) % 2 == 0 )) && VOLUME="A" || VOLUME="B"

回答by édouard Lopez

Your problem is the use of -eqoperator in the context of an arithmetic test (the double parenthesis).

您的问题是在-eq算术测试(双括号)的上下文中使用运算符。

You need Command Substitution $(…):

您需要命令替换$(…)

if (( $(date +%U) % 2 == 0 )); then VOLUME= "A" else VOLUME="B"; fi

N.B.: Why is $(…)preferred over `…`(backticks)?

注意:为什么$(…)优先于`…`(反引号)?

回答by tripleee

Try this.

尝试这个。

case $(date +%U) in
    *[02468] ) VOLUME="A";;
    *) VOLUME="B";;
esac

Note also that spaces around =are not permitted.

另请注意,周围=不允许有空格。