Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing 2 text strings

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I need to be able to compare 2 text strings
ex

a=test
b=test

if [ $a -eq $b ]
then
echo "Hurra"
else
echo "Bummer"
fi

But this doesn't work!

Thanks
 
Code:
if [ "$a" = "$b" ]
works on my systems. //Daniel
 
Hi,

the following operators compare strings and integers respectively (which might be a bit confusing) in Korn Shell scripts:

#!/bin/ksh

var1=1
var2=1.0

# string comparison:

[[ $var1 = $var2 ]] && echo "they are equal as strings..." || echo "they are not equal as strings..."

# integer comparison

[[ $var1 -eq $var2 ]] && echo "they are equal as integers..." || echo "they are not equal as integers..."

exit 0

I hope this small example helps

mrjazz [pc2]
 
Hi

For numeric comparisons use
-eq -ne -lt -gt -le -ge

for string comparisons use the operators
= < > !=

 
for strings:
case $first in $second) echo EQUAL
;; *) echo NOT EQUAL
;; esac -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top