Why this behaviour of IF condition?

I have a variable, defndata, which is a number (fetched from a file using awk).

I want that if defndata is not initialized (that is its not found in the file using awk), then to execute a block of statements, otherwise execute another block.

if [ "$defndata" -ne "" ]
then
....
else
...
fi

Now this statement is working fine if the value is not found, and defndata is blank.
However, if the value found is 0 (zero), its still treating it as a blank ("") and else condition block is being executed. If any other number is being found, it executes the first block (as expected).

I am not able to understand why?:confused:

Note: I am using ksh.

Try using:

if [[ -n "$defndata" ]]
then
   ...
else
   ...
fi

Thanks. It worked. Could you explain me where i went wrong?

-ne (like -eq, -ge, ...) are for numerical comparisons. You are comparing strings.

Just as Annihilannic said.

Best use double brackets (most modern shells should support them), which uses the shell instead of invoking "test", whereas it is faster. Round brackets for arithmetic values and square chars/strings etc.

There are the options -n for "not empty" and -z for "zero" for testing which make it more safe to not get the problem you had.