Dhinakaran Ramu
Dhinakaran Ramu

Reputation: 11

Unable to compare a string in if else statement in unix

cat sample.sh
a=$1

if [ "$a" = "Dhinakaran Ramu" ];then

        echo "Present"
else

        echo "Not Present"
fi

 sample.sh Dhinakaran Ramu

Answer is "Not Present"

Upvotes: 1

Views: 37

Answers (3)

Asensi
Asensi

Reputation: 188

It worked executing

sample.sh "Dhinakaran Ramu"

Note: if we execute

sample.sh Dhinakaran Ramu

then the script receives not one argument, but two. In the script you use $1, because you expect one argument.

Upvotes: 1

Nima Hejazi
Nima Hejazi

Reputation: 151

Make sure when you run the script, send the text in quotation marks:

$ ./sample.sh "Dhinakaran Ramu"

Upvotes: 1

Yuri Ginsburg
Yuri Ginsburg

Reputation: 2601

When you run the script $1 is Dhinakaran and $2 is Ramu. Try

sample.sh "Dhinakaran Ramu"

Upvotes: 0

Related Questions