J.Mishra J.Mishra - 2 months ago 14
Bash Question

Compare two alphanumeric variables mixed with special expression in shell script

I want to compare two alphanumeric strings and below is the bash expression I think should do the job. But I'm getting blank result. Please advise.

:~$ echo $tempnow $threshold
+60.0°C +80.0°C

:~$ res=`echo "$tempnow $threshold" | awk '{ if($1 > $2) print "Exceeds"; else echo "Normal" }'`
:~$ echo $res

:~$

Answer

There is no echo in awk.

You can use:

echo '+90.0°C +80.0°C' | awk '{ print ($1+0 > $2+0 ? "Exceeds" : "Normal") }'
Exceeds

echo '+60.0°C +80.0°C' | awk '{ print ($1+0 > $2+0 ? "Exceeds" : "Normal") }'
Normal

Also note use of +0 to convert fields into numeric values.

Comments