[root@mypbxadmin ~]# if [ 4.2 -gt 3.4 ]
> then
> echo "1"
> else
> echo "2"
> fi
-bash: [: 4.2: integer expression expected
2
How can i compare floating point numbers inside statement?
[root@mypbxadmin ~]# if [ 4.2 -gt 3.4 ]
> then
> echo "1"
> else
> echo "2"
> fi
-bash: [: 4.2: integer expression expected
2
How can i compare floating point numbers inside statement?
You don't. The test command cannot deal with floating-point numbers.
Convert them to integers or use awk:
if awk -v x=$1 -v y=$2 'BEGIN { exit (x > y) ? 0 : 1 }'
then
echo 1
else
echo 2
fi
I am not an expert in awk. So i need more help. I have a variable named "VAR1" that stores a floating point number. I want to compare it with "12".
if [ $var1 -gt 12 ]
then
echo "1"
else echo "2"
fi
What should be the corresponding code in awk.
---------- Post updated at 04:32 PM ---------- Previous update was at 04:28 PM ----------
Should it be something like:
if awk -v x=$var1 -v y=12 'BEGIN { exit (x > y) ? 0 : 1 }'; then echo 1; else echo 2; fi
or with 'bc':
#!/bin/ksh
a=1.72
b=1.71
if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
echo ">"
else
echo "<"
fi
For that, you don't need awk:
if [ $(( ${var1%.*} + ${var1#*.} )) -gt 12 ]
then
echo "1"
else echo "2"
fi
If the decimal portion of $var1 begins with 08 or 09, it'll have to be massaged.
If the decimal portion of $var1 begins with 08 or 09, it'll have to be massaged.
Didn't understand this point clearly.
Try it and see.
ksh93 is my favourite shell for scripting :
if (( 4.2 > 3.4 )); then
echo 1
else
echo 2
fi
It may be; it may not be.
That's why it is better to test for the features you need.
Ya it works. But how to determine the version of the KornShell?
echo ${.sh.version}
$ echo ${.sh.version}
/bin/ksh: : bad substitution
OK OK
(echo ${.sh.version}) 2>&- || echo ksh88
Inside a Korn shell.
[root@wiki ~]# ksh
# (echo ${.sh.version}) 2>&- || echo ksh88
Version M 93s+ 2008-01-31
---------- Post updated at 06:08 PM ---------- Previous update was at 06:07 PM ----------
# echo ${.sh.version}
Version M 93s+ 2008-01-31
That is a very recent ksh93 . In fact it is ksh93s+ (it has a couple of new features).