In shell script how can we compare (integer and floating point) ,(flaoting point and floating point),(floating point and integer),(integer and integer) with only one if condition.
i have few examples like
set X=3.1
set Y=4.1
if [ $X < $Y ] then
echo "wassup"
endif
But running the above from cron job doesnt seem to work.
EDIT. based on the comments to this answer (thanks to user unknown and glenn jackman), it seems that when using
bc
for a true/false test, the requiredbash
test is simply:(( $(echo "$X < $Y" |bc) ))
... see the test results and script belowwheras, the comparison to
-ne 0
is needed for the old style bash[ ]
test.bash
does not natively handle floating point numbers, but you can call a utility such asbc
From
man bc
- An arbitrary precision calculator languageTEST results:
TEST script:
below example works on bash shell.
you may want to learn shell script here
The way to carry out floating point operations in bash is to use bc which is available on almost all linux distributions.
There's a good article available on linux journal about floating point math in bash using bc.
Bah itself only handles integers. Use bc:
You don't need to worry about scale. It is just for the preocision of output formats: