Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

how do i compare strings???

5 posters

Go down

how do i compare strings??? Empty how do i compare strings???

Post by akalya 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

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

Post by Christopher 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
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

Post by Saradha Kannan 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

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

Post by Saradha Kannan 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

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

Post by Mekala 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

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

Post by Christopher 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.
Christopher
Christopher
Admin

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

https://unixcpp.forumotion.com

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

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

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

akalya

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

Back to top Go down

how do i compare strings??? Empty Re: how do i compare strings???

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

test ! $publisher will work as well!

Maithreyi

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

Back to top Go down

how do i compare strings??? Empty 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