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

人走茶凉 提交于 2019-12-02 02:48:34

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.

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.

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)

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