A nice way to check a string

Guys,
I need some advice on how to check a string, which may or may not have a entry.. Never really worked out how to do this.. May be a good time to learn now.

This is what i am trying to do

Run a command, to return a string
If the string is not empty , then run the if statement, otherwise ignore

How can i write the code, so it does not give out a error message. in the form of " [: argument expected"

Thanks - Code and Output below:

CODE
LDN_MEDIA_SERVER_HOST=`$ADM_CMD/bpimagelist -backupid $BACKUP_ID  | awk 'NR==1{print $(10)}' | sed -e "s/\./ /g" | awk '{print $1}' | tr '[A-Z]' '[a-z]'`
   if [ $LDN_MEDIA_SERVER_HOST != " " ] ; then
     LDN_MEDIA=`$ADM_CMD/bpimagelist -backupid $BACKUP_ID  | awk 'NR==1{print $9} | awk 'NR==1{print $(1)}''`
   fi
Output
+ LDN_MEDIA_SERVER_HOST=''
+ [ != ' ' ]
./EOM_FINAL_VERIFY_AND_REMOVE_COPY1[102]: [: argument expected

hi, try:

if [  -z $LDN_MEDIA_SERVER_HOST ] ; then
[...]

-z tests if the string length is zero

see ya
fra

That doesn't seem to work!!!!!

---------- Post updated at 10:07 AM ---------- Previous update was at 10:06 AM ----------

That doesn't seem to work!!


+ LDN_MEDIA_SERVER_HOST=''
+ [ -z ]

---------- Post updated at 10:27 AM ---------- Previous update was at 10:07 AM ----------

Used -n instead and worked.....

Both "test -z" and "test -n" need the string variable in double quotes:

These two tests are identical:

if [  -z "${LDN_MEDIA_SERVER_HOST}" ] ; then

if [  ! -n "${LDN_MEDIA_SERVER_HOST}" ] ; then

Note that if you use the Korn shell's built-in test ( [[ ]] ) instead of the test command, you don't need to quote the variable in order for test to "see" that value:

#!/usr/bin/ksh

EFS=

# Test for null.
if [[ -z $EFS ]]; then
  print a
else
  print b
fi

exit 0

Output:

$ iftest
a
$

However, it's probably a good habit to quote it anyway in case you use another shell that does not work this way. Plus, it may help the person behind you maintaining your code. Actually, that's a good tip, while coding keep in mind the poor guy after you that has to maintain your code. :slight_smile: The more time someone has to spend figuring out your tricky code, the more expensive that program has become to maintain. Make it clear and use comments assuming the person behind you is less experienced.