Shell script execution error [: 0: unary operator expected

When executing the shell script, the if judgment statement reports an error

#!/bin/bash

if [ 0 == $testNum ]
then
    echo "Check Result:0"
fi

  

Testnum is not defined when it is used, and no assignment is empty (it was originally defined, but it is written incorrectly, similar to tsetnum). After the conversion, it is actually

if [ 0 ==  ]

Unequal and less [sign errors; Avoid the following

#!/bin/bash

if [[ 0 == $testNum ]]
then
    echo "Check Result:0"
fi

  Use “[[]]” to judge; Or add a character

#!/bin/bash

if [ "0"x == "$testNumx"x ]
then
    echo "Check Result:0"
fi

  

In fact, for my mistakes, of course, it’s better to use “[]” ordinary judgment, and you can check the mistakes; However, this implementation is mostly used to judge whether the query value or the value given by other conditions meet the conditions. It is a normal scenario if it is empty, so you should improve your code

Detailed meaning to be added

Similar Posts: