how do i compare strings???
5 posters
how do i compare strings???
In my program,user will have to enter some input and i have to make sure that it is not blank(some input must be there)....
i tried this but it gives me error:
echo "enter publisher"
read publisher
while test $publisher == " "
do
echo "publisher cannot be blank enter again"
read publisher
done
output:
enter publisher
validate: line 26: test: ==: unary operator expected
any ideas????
i tried this but it gives me error:
echo "enter publisher"
read publisher
while test $publisher == " "
do
echo "publisher cannot be blank enter again"
read publisher
done
output:
enter publisher
validate: line 26: test: ==: unary operator expected
any ideas????
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: how do i compare strings???
This code ll give required output. but i don know why it is not possible to compare strings??
- Code:
echo "enter publisher"
read publisher
cnt=`echo $publisher | wc -c`
while test $cnt -eq 1
do
echo "publisher cannot be blank enter again"
read publisher
cnt=`echo $publisher | wc -c`
done
Re: how do i compare strings???
echo "enter publisher"
read publisher
while test -z $publisher
do
echo "publisher cannot be blank enter again"
read publisher
done
OUTPUT
sh str.sh
enter publisher
publisher cannot be blank enter again
sara
Here, the -z will check if the length of the string that is tested is zero or not...
read publisher
while test -z $publisher
do
echo "publisher cannot be blank enter again"
read publisher
done
OUTPUT
sh str.sh
enter publisher
publisher cannot be blank enter again
sara
Here, the -z will check if the length of the string that is tested is zero or not...
Saradha Kannan- Posts : 25
Points : 25
Join date : 2010-03-04
Re: how do i compare strings???
In test cmd..
-z STRING
the length of STRING is zero
-z STRING
the length of STRING is zero
Saradha Kannan- Posts : 25
Points : 25
Join date : 2010-03-04
Re: how do i compare strings???
@christopher
why does cnt display the value 1
why does cnt display the value 1
Mekala- Posts : 7
Points : 12
Join date : 2010-03-04
Re: how do i compare strings???
even if u enter 3 letters, it ll give u 4 as ouput. bcoz wc includes the EOF character too.
Re: how do i compare strings???
@saradha n @christopher
thanks it works!!!!
thanks it works!!!!
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: how do i compare strings???
test ! $publisher will work as well!
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum