Sammidbest
Sammidbest

Reputation: 515

Issue while comparing variables in shell script

I am new to shell scripting,any help will be appreciated!!

I am working on a .sh file I want to check if the values in 3 variables are same ,then do something Here is my code:

#!/bin/sh +x 
echo "OID host name= $OID_HOST_NAME"
echo "OIM host name=$OIM_HOST_NAME"
echo "OHS host name=$OHS_HOST_NAME"
echo "Printing done"
if ["$OID_HOST_NAME" == $OIM_HOST_NAME ]&& ["$OIM_HOST_NAME" == $OHS_HOST_NAME ]; then
        nodelist=ALL
echo "Inside if $nodelist"
else
echo "Inside else $nodelist"

fi

I am getting syntax error on the if condition. utils/main.sh: line 67: [slc06wmg.us.oracle.com: command not found

I have tried with -eq operator but same error.

Kindly help!!

Upvotes: 0

Views: 85

Answers (1)

Austin Hastings
Austin Hastings

Reputation: 627

You need a space after the '['.

The shell programming context is a little weird in that everything is considered to be a command line. So all variable substitution is really "text" substitution, and the resulting text is then parsed as a command line.

Your if statement gets substituted as:

if ["$OID_HOST_NAME" ...

if [slc06wmg.us.oracle.com ...

And then the shell tries to identify a command called "[slc06wmg.us.oracle.com", which causes the error you see.

If you insert a space, you'll have:

if [ slc06wmg.us.oracle.com ...

which the shell will parse as "if", "[", and then a parameter "slc06wmg.us.oracle.com" which is passed to the "[" command.

(Back in the day, "[" was actually a symbolic link to /bin/test. Now, "[" is understood internally by most shells.)

You can also use this behavior to your advantage. For example, to concatenate strings you just put them next to each other:

filepath=$directory/$filename

if [ -r $filepath ] ...

Just make sure there are spaces around everything that needs spaces. :)

Upvotes: 1

Related Questions