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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare two string variables?

Status
Not open for further replies.

signalsys

Technical User
Sep 5, 2005
44
CN


two string variables:
var1="0123'
var2='01BD'
What syntax should i use to compare the two variables and output the bigger one?

Thanks in advance.
 
You say you want to output the bigger one. This implies that the strings are, in fact, hex digits. I'm not sure you can compare hex digits in ksh.

In general terms the symbols > < = <= >= refer to string comparison and -lt -le -eq -gt -ge refer to numeric comparison

so
Code:
# print numeric biggest var
[[ $var1 -gt $var2 ]] && echo $var1 || echo $var2
# print dictionary bigest var
[[ $var1 > $var2 ]] && echo $var1 || echo $var2
but neither work for hex.

Ceci n'est pas une signature
Columb Healy
 
If you're comparing hex values, you can try this

Code:
var1=`echo "16i 0123 p" | dc`
var2=`echo "16i 01BD p" | dc`

[[ $var1 -gt $var2 ]] && echo $var1 || echo $var2

Regards,
Chuck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top