views:

356

answers:

5

Hello,

i am testing with the shell script below:

#!/bin/ksh -x


instance=`echo $1 | cut -d= -f2`
if [ $instance == "ALL" ]
then
echo "strings matched \n"
fi

It's giving this error in the if condition:

: ==: unknown test operator

is == really not the correct syntax to use? I am running on the command line as below

test_lsn_2 INSTANCE=ALL

Could anybody please suggest a solution. Thanks.

A: 
totest=$1
case "$totest" in
  "ALL" ) echo "ok" ;;
  * ) echo "not ok" ;;
esac
ghostdog74
+2  A: 

I see that you are using ksh, but you added bash as a tag, do you accept a bash-related answer? Using bash you can do it in these ways:

if [[ "$instance" == "ALL" ]]
if [ "$instance" = "ALL" ]
if [[ "$instance" -eq "ALL" ]]

See here for more on that.

Alberto Zaccagni
thanks for the reply monte. as andre miller said == is not working = is working.i will accept ur answer but also +1 for andre
Vijay Sarathi
+5  A: 

To compare strings you need a single =, not a double. And you should put it in double quotes in case the string is empty:

if [ "$instance" = "ALL" ]
then
    echo "strings matched \n"
fi
Andre Miller
thanks andre for your suggestion
Vijay Sarathi
A: 

I'va already answered a similar question. Basically the operator you need is = (not ==) and the syntax breaks if your variable is empty (i.e. it becomes if [ = ALL]). Have a look at the other answer for details.

soulmerge
A: 

Try

if [ "$instance" = "ALL" ]; then

There were several mistakes:

  1. You need double quotes around the variable to protect against the (unlikely) case that it's empty. In this case, the shell would see if [ = "ALL" ]; then which isn't valid.

  2. Equals in the shell uses a single = (there is no way to assign a value in an if in the shell).

Aaron Digulla