tags:

views:

2412

answers:

5

While attempting to write a simple bash script to help with my deployment process, I ran in to a confusing error:

#!/bin/bash
WEEKDAY=$(date +'%u')
echo $WEEKDAY
DAYS_TO_WEDNESDAY=$((3-$WEEKDAY))
echo $DAYS_TO_WEDNESDAY

Results in:

1
")syntax error: invalid arithmetic operator (error token is "

The strangest part of it is that I could swear that this very script ran perfectly well some days ago.

EDIT (14:58):

The issue was not with the bash script, but with using SVN through TortoiseSVN. The detour through Windows changed EOL markers to "CR LF", which result in syntax errors in bash. Setting svn:eol-style -property helps avoid further similar issues.

+1  A: 

You can omit the $ within an arithmetic expression.

So:

DAYS_TO_WEDNESDAY=$((3 - WEEKDAY))
slim
Wow, you learn something new every day.
jhs
+1  A: 

Copied and pasted your code and it works fine:

$ cat > test.sh
#!/bin/bash
WEEKDAY=$(date +'%u')
echo $WEEKDAY
DAYS_TO_WEDNESDAY=$((3-$WEEKDAY))
echo $DAYS_TO_WEDNESDAY
^D

$ bash test.sh 
1
2

Maybe $WEEKDAY had bad data in it?

jhs
Thanks for the info. I ended up rewriting the script and that one worked beautifully. hexdump revealed that the detour through windows SVN client had added CR LF -EOLs instead of just LF:s.
Jukka Dahlbom
A: 

I get almost that exact same error if WEEKDAY isn't set when I hit the DAYS_TO_WEDNESDAY calculation:

-bash: 3-: syntax error: operand expected (error token is "-")
Paul Tomblin
+1  A: 

Your error message seems to indicate pollution of your data with CRs.

")syntax error: invalid arithmetic operator (error token is "

Notice how the stuff that's supposed to come after the current end of your line is at the beginning. That's most likely because your error token is in fact a CR (which is a carriage return character - a character that instructs the terminal to put the cursor on the beginning of the line). These characters are almost only used by Windows machines where they are part of line endings.

I will assume that you're working on a windows machine and that your "date" command gave the output followed by a "windows" newline, which is actually a \r\n (carriage return, newline). The $() always strips trailing newlines, which leaves the \r at the end causing parsing problems in your script.

Here, the following command produces your error on UNIX:

$ foo=$'5\r'; echo $((5+foo))
")syntax error: invalid arithmetic operator (error token is "

To resolve the issue, you need to get rid of the \r in your data. You can use parameter expansion for this, or tr(1).

$ foo=$'5\r'; echo $((5+${foo//$'\r'}))
10

$ $ foo=$'5\r'; echo $((5+$(tr -d '\r' <<< "$foo")))
10
lhunath
A: 

Thank you so much for this lhyunath, this was driving me mad (I was trying to do some shell arithmetic in Cygwin / BASH and this had me stumped as well).

Belated congratulations on your baby!!

lra/lynysys.com