[PLUG] How to test a string against a blank string in shell script

Shreerang Patwardhan patwardhan.shreerang at gmail.com
Thu Mar 25 17:43:19 IST 2010


Hey all,
     I found the solution to my problem which was as stated below:

    How do we compare a string with a blank string.
Example: String 1 = test
                String 2 =

I have written a small script. Can anyone guide me to the error; if any; in
it?

Script:

echo $1 $2

if [ "$2" -a "" ]
then
    echo 1
else
    echo 0
fi


Output:
Case I:

./test.sh 10 20
10 20
0

Case II:

./test.sh 10
10
0

The required correction in the code is as follows:

if [ "$2" -a  ]    #Blank string should be treated as a white-space and not
as ""
then
    echo 1
else
    echo 0
fi


Cheers!!
Shreerang Patwardhan.



More information about the Plug-mail mailing list