how to write string comparison in shell script

xewoox
xewoox used Ask the Experts™
on
Hi, I am writing a shell script which will input a string parameter. Inside the script I will do different action depending on the string value. I wrote

myscript:

if [ $1 == "test1" ]
then
  echo A
else
  echo B
fi

myscript  test1
A

myscript junk
B

So, this is all good

now if I change my script to

if [ $1 == "no" ]
then
  echo A
else
  echo B
fi

myscript  'asdfasdf'
B
myscript '-plll -o lll'
A

Why A?

I enter most garbage parameter to myscript it will echo B but for a few it will return A. Why?

What do I need to do to fix this? Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try without quotes
myscript  -plll -o lll
Top Expert 2007
Commented:
try

#!/usr/bin/sh
if [ "$1" = "no" ]
then
  echo A
else
  echo B
fi
Top Expert 2007
Commented:
The reason your second example outputs 'A' is that the if test effectively becomes

if [  -plll -o lll == 'no' ]

As -plll has no comparison, it is automatically a true value, therefore you end up with

true OR false

which is TRUE.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial