System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Numeric comparaison with ksh

SOLVED
Go to solution
Leo The Cat
Regular Advisor

Numeric comparaison with ksh

Hello

I've a script with something like this :

SA=$(df -m -P | grep -i /logs | awk '{print $4}')


if [[ $SA < 900.25 ]] ;then
...


Here, is it a numeric compare or a string compare ?

Bests regards
Den
6 REPLIES
Leo The Cat
Regular Advisor

Re: Numeric comparaison with ksh

I think that I could the necessary -le as

if [[ $SA -le 900.25 ]] ;then

correct in any situation?

Regards
Den
Leo The Cat
Regular Advisor

Re: Numeric comparaison with ksh

sorry -gt

;-)
James R. Ferguson
Acclaimed Contributor
Solution

Re: Numeric comparaison with ksh

Hi Den:

http://www.docs.hp.com/en/B2355-60130/ksh.1.html

For shells, the mathmatical symbol operators compare *strings* and the English-like ones (-lt, -gt, -le, -ge, -eq -ne) perform *numeric* comparisons.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Numeric comparaison with ksh


Hmm, what version of Unix is that ?!

HP-UX 11.31 does not have a -m option for df, only -k.

Why not have awk do the grepping and the comparing?
A shell is not going to do the decimal fractions.
Awk is happy to.

What will your scipt do when someone mounts /logs_backup or some such?

SA=$(df -m -P | awk -i '/\/logs$/{print int ($4 - 900.25) }')

Note: hpux does not have a -i with the standard awk. gawk has that. either speel out the mount point in proper casing or activate IGNORECASE or lowercase() the column.

fwiw,
Hein.
Dennis Handly
Acclaimed Contributor

Re: Numeric comparaison with ksh

>is it a numeric compare or a string compare?

As mentioned, this is a string compare unless you have it within arithmetic expressions, (( )) or use awk(1). And even there it will fail since ksh and sh don't do floating point. You need ksh93:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1151323

>Hein: activate IGNORECASE or lowercase() the column.

HP-UX's awk doesn't have IGNORECASE or lowercase() but has tolower().
Leo The Cat
Regular Advisor

Re: Numeric comparaison with ksh

Thanks guys.