how do i compare strings???

Go down

how do i compare strings???

Post by akalya on Wed Mar 17, 2010 12:48 pm

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

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Christopher on Wed Mar 17, 2010 1:26 pm

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
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: how do i compare strings???

Post by Saradha Kannan on Wed Mar 17, 2010 1:34 pm

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

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Saradha Kannan on Wed Mar 17, 2010 1:43 pm

In test cmd..
-z STRING
the length of STRING is zero

Saradha Kannan

Posts : 25
Points : 25
Join date : 2010-03-04

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Mekala on Wed Mar 17, 2010 1:49 pm

@christopher
why does cnt display the value 1

Mekala

Posts : 7
Points : 12
Join date : 2010-03-04

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Christopher on Wed Mar 17, 2010 1:52 pm

even if u enter 3 letters, it ll give u 4 as ouput. bcoz wc includes the EOF character too.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: how do i compare strings???

Post by akalya on Wed Mar 17, 2010 1:53 pm

@saradha n @christopher
thanks it works!!!!

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Maithreyi on Wed Mar 17, 2010 1:55 pm

test ! $publisher will work as well!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: how do i compare strings???

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum