Tuesday, January 8, 2013
11:00 PM

Bash Script: Performing Floating point Math Operation

NOTE: If your program depends heavily on Math operation (floating point), better switch to ksh instead of traditional bash.

Bash does not support floating point operations but it's possible to redirects these Math operation to some other program (bc).

The "bc" calculator comes as a part of your Linux distro, so there's no need for you to install anything extra. In addition to performing simple math functions, it can also perform conversions between different number systems, perform a number of scientific math functions, and can even run programs that you write and save in a text file. look the bc man pages for more details.

Below is simple bash script which demonstrate the usage of bc commands in the script

Source: cat floating.sh
#!/bin/bash

echo "----- Addition ------"
x=10.2
y=20.2
z=`echo $x+$y|bc`
echo $z
Continue Reading...

0 comments:

Post a Comment