Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 463
Augusto Avatar asked Dec 12 '13 20:12

Augusto


2 Answers

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

You can use this command:

(( $(date +%U) % 2 == 0 )) && VOLUME="A" || VOLUME="B"
like image 97
anubhava Avatar answered Nov 03 '22 19:11

anubhava


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

You need Command Substitution $(…):

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

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

like image 37
Édouard Lopez Avatar answered Nov 03 '22 19:11

Édouard Lopez