For numbers between 0 and 1 the below logic is not working.
Output of above shall be "correct" but its echoing "incorrect".Kindly suggest
a=.1
if [ $a -gt .01 ]
then
echo correct
else echo incorrect
fi
For numbers between 0 and 1 the below logic is not working.
Output of above shall be "correct" but its echoing "incorrect".Kindly suggest
a=.1
if [ $a -gt .01 ]
then
echo correct
else echo incorrect
fi
Use a shell that supports floating point arithmetic (ksh93, zsh) or use some other tool (awk, Perl ...).
zsh-4.3.14[sysadmin]% ksh93 -c '(( .1 > 0 )) && print ok'
ok
zsh-4.3.14[sysadmin]% zsh -c '(( .1 > 0 )) && print ok'
ok
zsh-4.3.14[sysadmin]% bash -c '(( .1 > 0 )) && echo ok'
bash: ((: .1 > 0 : syntax error: operand expected (error token is ".1 > 0 ")
zsh-4.3.14[sysadmin]% awk 'BEGIN { print (.1 > 0) ? "ok" : "ko" }'
ok
Bash does not understand floating point arithmetic..
try this
# awk 'BEGIN{a=.1;if(a>.01)print "correct";else print "incorrect"}'
correct
in ksh
a=.1;if (( $a > .01 )) then echo correct ; else echo incorrect ; fi
correct
a=.1
if (( $(echo "${a} > .01" | bc) == 1 )); then
echo correct
else
echo incorrect
fi