1
CorrectInstance=DEV
Instance_Name=DEV
if [ $Instance_Name == $CorrectInstance ]
then
   echo "Instance validated sucessfully"
else
   echo "Instance validation failed. Script exited."
   exit
fi

When I am running above script I am getting below error.

Match.sh[3]: ^M: not found [No such file or directory]
Match.sh: line 3: syntax error at line 10: `fi' unexpected

Please sugeest

  • Unrelated to the immediate problem, this should be `[ "$Instance_name" = "$CorrectInstance" ]`. That is to say, `=` rather than `==` (as the latter is an extension not defined by the POSIX sh standard), and quotes around all expansions (so spaces, glob characters, etc. inside your test command can't break its syntax). These issues can be found automatically by http://shellcheck.net/ – Charles Duffy May 12 '15 at 19:33

2 Answers2

10

^M is also \r So probably you had this on a Windows system at some point and edited it there where the end of line sequence is \r\n. Try running dos2unix on it if you have it available, otherwise you can use a tool like sed to remove whitespace from the ends of lines.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
0

Depending on the unix, I think that "==" is wrong.
It is supposed to be one equal sign. Note the spaces around the equal sign.
If you run it without those spaces it might assign a variable and return true instead of running any test.

Put the variables in quotes too. That way if they are missing you have "" instead of a syntax error.

And I use those {} just out of habit.

If I have $XY=stuff

print ${XY}123 returns stuff123

print $XY123 returns Nothing because variable $XY123 is not defined.

Redoing your bombed out line 3.
if [ "${Instance_Name}" = "${CorrectInstance}" ]; then

blah blah blah
clonea
  • 39
  • 4