Shell script error: ‘[: =: unary operator expected’

Shell script error: [: =: unary operator expected “2009-08-11 18:00

When the matching strings are equal, I use a statement like this: if [$status = = OK]; Then echo “OK” fi in the run-time [: =: unary operator expected error, has been unable to find the reason, tried to delete the space on both sides of the equal sign and the space in brackets do not work, finally Baidu found the reason. Change the statement so that there is no error: if [[$status = $OK]]; The reason is that if the value of the variable status is empty, it will become [= “OK”]. Obviously [it is not equal to “OK” and lacks [symbol], so this error is reported. Of course, errors are not always made. If the value of the variable status is not empty, the program will be normal, so such errors are very hidden. Or the following method can be used to avoid this error: if [“$status” x = = “OK” x]; Of course, X can also be other characters. By the way, whether there are double quotes in the shell is consistent in many cases

Similar Posts: