PDA

View Full Version : [ubuntu] linux cmds - to compare two words....



kimikrishna
February 22nd, 2009, 03:52 AM
i bought a linux commmands book to learn./

and now , i need to know how to compare two given words/.

like


$word1=kimi
$word2=krishna

now how to compare word1 and word2 ,
and show the result as "both are same" or "both are different"

Old *ix Geek
February 22nd, 2009, 04:27 AM
Just test the two values:


word1=kimi
word2=krishna
if [ "$word1" = "$word2" ]
then echo "They're the same"
else echo "They're different"
fi

kimikrishna
February 22nd, 2009, 04:33 AM
if [ "$word1" = "$word2" ]


after i enter this,
each line comes with > ,
>
>
>
and i dont know what to do more

kimikrishna
February 22nd, 2009, 04:37 AM
oh ! i understood.
i have to enter if , else commands there.....


But even i enter two words are same....

it says "different"

Old *ix Geek
February 22nd, 2009, 04:38 AM
That's just a secondary prompt; type each line and hit [Enter]. After the line containing 'fi' you'll go back to your regular prompt.

Old *ix Geek
February 22nd, 2009, 04:42 AM
You're probably not typing it exactly as shown! Are you leaving off the dollar sign, $?

Make sure that you DO NOT use a $ when defining the values for each variable, but DO use a $ when comparing their values.

Just type it exactly as shown and post back!

kimikrishna
February 22nd, 2009, 04:52 AM
thanks..

i got it !!

ubu rocks !

Old *ix Geek
February 22nd, 2009, 04:53 AM
You're welcome. :)