If statement for null

Hi,

I want to be able to check if a variable is not equal to null. I am using KSH, and am getting this error message when i run this script:

: assignment requires lvalue

The line which is causing the problem is as follows:

 
if (($SFTP_DESTINATION != '' ));then
if ssh ${SFTP_ACCOUNT}@${SFTP_ADDR} test ! -d ${SFTP_DESTINATION};then
    error_exit "Directory does not exist on Remote Host"
fi
fi

Please can someone point me in the right direction on how i can check for null values?

Thanks,

if [ -z "$STRING" ] 

tests if a string is NULL. The quotes are important -- without them a null string becomes [ -z ] which is a syntax error.

Same below, you need to quote things.

if ssh "${SFTP_ACCOUNT}"@"${SFTP_ADDR}" test ! -d "'${SFTP_DESTINATION}'" ; then

Note the single-quotes inside double-quotes around sftp_destination. That keeps it in single quotes even when sent to the remote server.

Ok, thanks for the reply. That now works fine! Thanks

1 Like

For the sake of argument, the ksh built=in test "[[" handles a null variable. At least on our Sun box. Consider this:

#!/bin/ksh

unset efs
#efs=
#efs=x

if [[ $efs == "x" ]]; then
  print "efs equals 'x'"
else
  print "efs is unset, efs != 'x' or efs is null"
fi

Output:

$ ./testprog
efs is unset, efs != 'x' or efs is null
$

Of course if you explicitly need to test for null by itself, by all means use '-z' (or '-n' for not null) as testing like this is really testing for 3 conditions at once and may not be the desired action.

Does it handle a variable containing a string with spaces?

It would appear so:

#!/bin/ksh

efs="   "   # 3 spaces

if [[ $efs == "   " ]]; then
  print "efs equals 3 spaces"
else
  print "efs is unset, efs != 3 spaces or efs is null"
fi

Output:

$ ./testprog
efs equals 3 spaces

Caution: Nitpick ahead. :wink:

That may be true of older implementations of test/[, but, for contemporary implementations, while the recommendation is sound, the analysis is incorrect.

A POSIX-compliant test/[, when passed a single argument, will succeed if the argument is not null, regardless of the value of that argument (i.e. it will never interpret the string as an operator of any kind).

When $STRING is null, [ -z $STRING ] reduces to a single, non-null argument, -z , and will always return true. There is no syntax error and the correct exit status is achieved through an unintended mechanism.

If $STRING is non-null and does not contain any IFS characters, again the correct exit status is returned.

The only problematic scenario occurs when the result of $STRING is split into multiple arguments, and this is why the quoting is sound advice.

Regards,
Alister

---------- Post updated at 12:16 PM ---------- Previous update was at 12:06 PM ----------

As a result of POSIX prescribing that test interpret its arguments in different ways depending on their number, we no longer have to resort to inelegant hacks to protect parameter expansion against collisions with test's operators. We can safely write

[ "$a" = "$b" ]

instead of

[ X"$a" = X"$b" ]

which is often seen in older sh scripts (and in some newer ones as well).

Regards,
Alister