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
Shell - syntax error in expression (error token is "0 ")
提问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 -eq
operator 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
回答by tripleee
Try this.
尝试这个。
case $(date +%U) in
*[02468] ) VOLUME="A";;
*) VOLUME="B";;
esac
Note also that spaces around =
are not permitted.
另请注意,周围=
不允许有空格。