Thursday, August 30, 2012

Shell script to check the null or empty string
 To check whether a string is null 


if [[ -z "$String" ]]then echo  "string is null"fi


To check whether a string is empty is little trick, many websites suggest a different way. Easy way to do is 

#remove all the blank spaces in the string and check for null.
trimString=`echo $title | sed 's/ //g'`


if [ "$trimString" = "" ]
then

 echo  "string is null"
fi

or

if [[ -z "$trimString" ]]
then

 echo  "string is null"
fi

Remove all the white spaces is necessary because an empty string may have 1 or 2 or n contiguous white spaces and to handle the string comparision for all these cases you need to remove the blank spaces and add check for null.

Please add Suggestions to improve the scripts if any :)