2

I am evaluating following following expression. And it always evaluates to true even if CMD is START or STARTED. Am I doing something wrong?

if [ "$CMD"="START" ]; then
    echo fi
fi
1

4 Answers 4

8

Try spaces

if [ "$CMD" = "START" ] ; then
     echo fi
fi
Sign up to request clarification or add additional context in comments.

10 Comments

To be perfect it needs [ ] turned into [[ ]] to make sure it's evaluated by bash itself :)
Can you give more context, if you think it always false, then, probably, something's wrong with your $CMD.
I am extracting the command string using following expression CMD=grep -i "CMD" $TPMSTATUS | cut -d":" -f2 where TPMSTATUS contains the string CMD and its value separated by :
So what is in "CMD"? It probably isn't what you think. Try echo "x$CMDx" to see if you're getting spaces or something before or after.
Found the problem mapping was CMD : "START", but when I remove the double quotes, it worked
|
1

You need to have spaces around the =.

#CMD="STARTED"
#if [ "$CMD" = "START" ] ; then echo fi; fi
#CMD="START"
#if [ "$CMD" = "START" ] ; then echo fi; fi
fi
#

1 Comment

@siri, then you are doing something different from what you say you're doing, because that's cut and pasted from my shell.
1

From http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html:

  [] vs. [[]]

Contrary to [, [[ prevents word splitting of variable values. So, if VAR="var with spaces", you do not need to double quote $VAR in a test - eventhough using quotes remains a good habit. Also, [[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames. Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[ "value" == val* ]].

So, try:

if [[ "$CMD" == "START" ]]; then
    echo "fi"
fi

Comments

0

You should add spaces:

if [ "$CMD" == "START" ]; then
...
fi

2 Comments

-eq is for int and numer types only. Not strings.
-eq is arithmetic, not for strings.

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.