Stupid question to check if variable is empty

Hi,
I am simply trying to check if the variable is empty in the code below but it isn;t working. Anything that I might have missed here

#Check if values in job card are not empty
title=`cat $filename | grep "TITLE:" | cut -d ":" -f3`
if [[ -z $title ]]
then 
echo "10:Title Empty" ":Fail">> $rptfile
itemfailed=`expr $itemfailed + 1`
else
echo "10:Title description present." ":Pass" >> $rptfile
itempassed=`expr $itempassed + 1`
fi

Sample File :

:COMMENT *----------------------------------------------------------
:COMMENT *       TITLE: 
:COMMENT *      SOURCE: test

Did you try to double quote the variable: "$title" ?

Yup.. Tried double quotes too. No change.

Here's the output

(rk3388) itahpdev=> title=`cat $filename | grep "TITLE:" | cut -d ":" -f3`
(rk3388) itahpdev=> if [[ -z "$title" ]]
> then
> echo "10:Title Empty" ":Fail"
> else
> echo "10:Title description present." ":Pass"
> fi
10:Title description present. :Pass
(rk3388) itahpdev=>
(rk3388) itahpdev=> echo $title

(rk3388) itahpdev=>

Hello nua7,

Kindly let us know the complete input for same, also os information will be helpful too for us to guide/help you.

Thanks,
R. Singh

Try single "["and "]"

Hi,
[ ] did not work.
@Ravi, I am working on HP9000 Unix servers.

(rk3388) itahpdev=> echo $SHELL
/usr/bin/sh

In your sample file, TITLE has a space and is not empty.

Thanks Rudy. Any idea how that can be handled ?

This should work :

if [ -z "$(echo $title)" ]

It seems wasteful to fire up another process for this (and for incrementing counters) especially if there is an input file possibly thousands of lines long, in which case this will be expensive.

Could you try:-

if [ -z "$title" -o "$title" = " " ]
then
   echo "Null or space"
else
   echo "Contents"
fi

If this works, fit it to your script. It might be worth adjusting the loop too to something more like this:-

#Check if values in job card are not empty
grep "TITLE:" $filename | cut -d ":" -f3 | while read title         # Handle multiple records if required
do
   if [ -z "$title" -o "$title" = " " ]
   then 
      echo "10:Title Empty" ":Fail"
      ((itemfailed=$itemfailed+1))                                  # In-process
   else
      echo "10:Title description present." ":Pass"
      ((itempassed=$itempassed+1))
   fi
done >> $rptfile                                                    # All output from loop appended to this file

One thing to be aware of is that the variable being read in the loop, i.e. $title will be null when you come out of the loop. You can get round this like this if you need to know the last value:-

echo "a\nb\nc" | while read val
do
   echo "Read $val"
   VAL=$val
done
echo "\$val is \"$val\""
echo "\$VAL is \"$VAL\""

Output will be:-

Read a
Read b
Read c
$val is ""
$VAL is "c"

I hope that this helps,
Robin

There is no evidence the OP is using a loop. In any case, thousands of microseconds range delays shouldn't really hurt that much.

This would miss the case where title contains more than one space character, or a tab, or any combination of the above.

Beware that your echo statement is non portable, you should use instead:

printf "a\nb\nc\n" [ while read ...

Beware too that you are assuming ksh behavior by expecting VAL to be set at the end of the loop. A POSIX shell is not required to keep the VAL variable as it is set in a pipeline.

Not only that, "echo" is also often a shell built-in command. So "echo" is not necessarily spawning a subprocess.

You could crunch it some more by stripping all trailing white-space:

if [ -z ${title#$*} ]

It finally turns out that your file has <CR><NL> line terminators, typical for MS Windows stuff. You'll have to get rid of those first.

title=$(tr -d $'\r' <file | grep "TITLE:" | cut -d ":" -f3)

(Used $(...) in lieu of deprecated backticks, btw)
Then you can test on empty string:

[ -z "${title%%[[:space:]]}" ] && echo 1 || echo 0

The %% removes the longest matching suffix pattern. Or, make use of the shell's behaviour to remove unnecessary spaces in unquoted strings:

[ -z $title ] && echo 1 || echo 0
1 Like