Why does the following IF condition in ksh always evaluate to true?

寵の児 提交于 2019-12-02 03:25:31

问题


Consider, below code works as expected:

if [[ $SOME_VARIABLE = "TRUE" ]]; then
   echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"."
fi

But when I remove the space surrounding the equality operator it always evaluates to 0 exit status (At least that's what I assume it must be returning as it is taken as true):

if [[ $SOME_VARIABLE="TRUE" ]]; then
   echo "Always true."
fi

UPDATE:

Just to confirm whether the issue lies with the equality operator or not:

#!usr/bin/ksh

SOME_VARIABLE=FALSE

if [[ $SOME_VARIABLE == "TRUE" ]]; then
   echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"."
fi


if [[ $SOME_VARIABLE=="TRUE" ]]; then
   echo "Always true."
fi


[kent@TEST]$ sh test.sh
Always true.

UPDATE:

Summary:

  1. Using = is the same as == above, but is obsolete.
  2. ALWAYS mind your spaces.

回答1:


From ksh(1):

Conditional Expressions.

   A conditional expression is used with the [[ compound command  to  test
   attributes  of  files and to compare strings.  Field splitting and file
   name generation are not performed on the words between [[ and ]].  Each
   expression  can  be constructed from one or more of the following unary
   or binary expressions:

   **string** True, if string is not null.

   ...

So the following expression is true:

[[ somestring ]]

Now consider your second example:

if [[ $SOME_VARIABLE="TRUE" ]]; then

Assuming $SOME_VARIABLE is "SOMETHINGNOTTRUE", this expands to:

if [[ SOMETHINGNOTTRUE=TRUE ]]; then

"SOMETHINGNOTTRUE=TRUE" is a non-zero length string. It is therefore true.

If you want to use operators inside of [[, you must put spaces around them as given in the docs (note the spaces):

   string == pattern
          True, if string matches pattern.  Any part  of  pattern  can  be
          quoted to cause it to be matched as a string.  With a successful
          match to a pattern, the .sh.match array  variable  will  contain
          the match and sub-pattern matches.
   string = pattern
          Same as == above, but is obsolete.



回答2:


Because the one argument form of the test is true if the string is not the empty string. Since the only argument ends in =TRUE it certainly isn't the empty string, so the test evaluates to true.

Space, the final frontier :-)

Always pay heed to your spaces and keep in mind the word splitting.




回答3:


Just to pile on, this is explicitly called out in the ksh man page (in the description of the test command):

Note that some special rules are applied (courtesy of POSIX) if the number of arguments to test or [ ... ] is less than five: if leading ! arguments can be stripped such that only one argument remains then a string length test is performed (again, even if the argument is a unary operator)

(emphasis mine)



来源:https://stackoverflow.com/questions/13955208/why-does-the-following-if-condition-in-ksh-always-evaluate-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!