Skip to content
Advertisement

Amazon CLI bash script on Ubuntu 16.04, “unexpected operator” on if statements (syntax looks correct)

I’m trying to troubleshoot issues I’m having with an Amazon CLI backup script that’s running on Ubuntu Server 16.04.

When I run the script manually I see that my if statements throw errors. Per request I’ve reduced my example to be minimal and verifiable. This script was running on another production server just fine. I’ve also run it through a shell syntax checker, from what I can see the syntax looks correct. What am I missing?

Also, the syntax checker wanted me to wrap all of my variables in double quotes. Why?

Error:

JavaScript

Example:

JavaScript

Advertisement

Answer

This breaks:

JavaScript

it has to be

JavaScript

but I suspect it’s just a typo as it would prevent your error message from appearing.

You are probably running this as follows:

JavaScript

which means that the #!/bin/bash line is ignored, and either Bash in POSIX mode or another shell altogether is run. == is not POSIX, so you have to change

JavaScript

to

JavaScript

or run it with

JavaScript

or just

JavaScript

Consider:

JavaScript

As for “why quote”, the summary is “because of word splitting and glob expansion”. The Wooledge wiki has a good article about it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement