Tag Archives: “[: =: unary operator expected”

The shell script reported an error: not sure operator expected

A statement like this is used when matching strings

if[  $ timeofday  = ” yes”];   then

   echo  ” Good   morning”

   exit   0

The reason for the error is: if the value of the variable timeofday is empty, the if statement becomes if  [  =” yes”  ], This is not a legal condition. To avoid this, we must quote the variable if  [ “$ timeofdat”=”yes”  ], In this way, even null variables provide legal test conditions, if  [  ” “=” yes”   ]

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