简体   繁体   中英

Bash: How to do a variable expansion within an arithmetic expression?

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.

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

You can omit the $ within an arithmetic expression.

So:

DAYS_TO_WEDNESDAY=$((3 - WEEKDAY))

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM