Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: =: command not found (Bash Script)

Tags:

bash

I found a nifty little shell script that I wanted to use from this website here. I have followed everything step-by-step, but receive the following error when running this on my CentOS box.

./deploy: line 3: =: command not found

Line 3 only contains...

$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"

I've tried toying around with a bit, but don't understand why it won't accept the "=" character. Is there something wrong with the script, or is it merely something different in the way that my server processes the script?

Thanks!

like image 621
Brian Schroeter Avatar asked Apr 14 '14 16:04

Brian Schroeter


1 Answers

Gah, that script is full of bad scripting practices (in addition to the outright error you're running into). Here's the outright error:

$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"

As devnull pointed out, this should be:

ERRORSTRING="Error. Please make sure you've indicated correct parameters"

A couple of lines down (and again near the end), we have:

echo $ERRORSTRING;

...which works, but contains two bad ideas: a variable reference without double-quotes around it (which will sometimes be parsed in unexpected ways), and a semicolon at the end of the line (which is a sign that someone is trying to write C or Java or something in a shell script). Use this instead:

echo "$ERRORSTRING"

The next line is:

elif [ $1 == "live" ]

...which might work, depending on whether the value of $1 has spaces, or is defined-but-blank, or anything like that (again, use double-quotes to prevent misparsing!). Also, the == comparison operator is nonstandard -- it'll work, because bash supports it in its [ ... ] builtin syntax, but if you're counting on having bash extensions available, why not use the much cleaner [[ ... ]] replacement? Any of these would be a better replacement for that line:

elif [ "$1" = "live" ]
elif [[ $1 == "live" ]]
elif [[ "$1" == "live" ]]

Personally, I prefer the last. The double-quotes aren't needed in this particular case, but IMO it's safest to just double-quote all variable references unless there's a specific reason not to. A bit further down, there's a elif [ $2 == "go" ] that the same comments apply to.

BTW, there's a good sanity-checking tool for shell scripts at www.shellcheck.net. It's not quite as picky as I am (e.g. it doesn't flag semicolons at the ends of lines), but it pointed out all of the actual errors in this script...

like image 63
Gordon Davisson Avatar answered Oct 20 '22 19:10

Gordon Davisson