Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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