Useful bash tips

Addition of two int number  with awk –

echo $a $b | awk ‘{sum= $1 + $2 ; print sum }’

Multiplication and division of two floating number  with awk –

echo $a $b | awk ‘{ printf( “%.3f\n”, ( $1 * 100 ) / $2 );}’

res=`echo $a $b | awk ‘{ sum = $1 * $2 ; per = sum / 100; print per}’`

Mathematical operation with bc

res=`echo “$sum * $avg / 100” |bc`

free_t=`echo “$free / 1024  / 1024” |bc`

condition with bc –

if [ $(echo “$a < $b”| bc) -eq 1 ]

It will return either 0 or 1 depending on true or false.

Mathematical operation with bash

avg=`echo $((( $sum * 60 ) / 100 ))`

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: