0

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!!

1 Answer 1

1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.