Operating System - HP-UX
1752773 Members
5101 Online
108789 Solutions
New Discussion юеВ

how to compare a null string in UNIX Shell

 
Suman_7
Frequent Advisor

how to compare a null string in UNIX Shell

All,

I need to compare a null string comparison like this:

if [ ${STAT} -ne 0 -o
"${file_date}" = " " ]; then
echo "file date is NULL"
fi

But my code is not working. ${STAT} is the exit status.

Please suggest me what am I doing wrong.. Thank You,
Suman
4 REPLIES 4
curt larson_1
Honored Contributor

Re: how to compare a null string in UNIX Shell

do a -z "${file_date}"

and if you have your shell patched with recent patches the proper syntax would be:

if (( $STAT} != 0 )) || [[ -z "${file_date}" ]] ;then
print "file date is null"
fi

[...] and echo are obsolete

and if you just want an error message

print ${file_date:?}

will do that for you
Sridhar Bhaskarla
Honored Contributor

Re: how to compare a null string in UNIX Shell

Hi Suman,

-z is what I use too.

I believe, Curt intended to write

if [[ ${STAT} != 0 ]] || [[ -z "${file_date}" ]] ;then
print "file date is null"
fi


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Suman_7
Frequent Advisor

Re: how to compare a null string in UNIX Shell

Thanks for your response but my file date has space between it. The file date is like file_date=May 20 14:40

if I do [[ -z "${file_date}" ] the program is till thinking that the file date is null.

Please tell what am I doing wrong here?

Thank You
Suman
Suman_7
Frequent Advisor

Re: how to compare a null string in UNIX Shell

I am able to get it working now.

Thank You for your help.

Suman