2

both [[ string1 == string2 ]] and [[ string1 = string2 ]] means equal. But some other guy told me their machine don't support ==. The error message is: syntax error: `==' unexpected.

Seems they use pretty old UNIX with korn shell and I'm using REHL5.2. Since I cannot access their machines, how do I find out which version start to support "=="?

Thanks

valpa
  • 319
  • 7
  • 15

2 Answers2

2

You can look at the value of $SHELL and see if it's set to the shell's executable. You could then test and branch on the value of it, or lack thereof; I don't know if this will be set on older shells.

If you don't need the advanced features of [[ then use [ instead. This web page explains the differences in detail. Also take a look at this related question.

If you are only going to compare strings with == then use the POSIX syntax for the greatest compatibility: [ string1 = string2 ] which uses a single equals sign.

Starfish
  • 2,716
  • 24
  • 28
0

If the user is in fact using a KSH/Korn shell, the problem may not be the ==, but instead the lack of double quotes, as many of the AT&T standards now take the == per the --posix or -o posix flags set. (These two flags force KSH to utilize posix standards)

Long story short, you should use: if [ "$instance" = "ALL" ]; then

The double quotes protect against cases of null or otherwise empty strings, which would most likely throw an error, even if syntax was correct.

Bonus: It's not that KSH lacks support for == it's that KSH lacks support for null and empty objects/returns.

For more information on this, you can check out the main KSH page, or Peter Seebach's book on Portable Shell Scripting or even the O'Reilly Bash Cookbook, and finally the Korn Shell book by O'Reilly which I happened to find a freely accessible short link of some of these logical operands here :)

Batman
  • 36
  • 4