Warning: Undefined variable $html in /usr/share/nginx/html/lib/plugins/tabinclude/helper.php on line 240

Warning: Cannot modify header information - headers already sent by (output started at /usr/share/nginx/html/lib/plugins/tabinclude/helper.php:240) in /usr/share/nginx/html/inc/Action/Export.php on line 106

Warning: Cannot modify header information - headers already sent by (output started at /usr/share/nginx/html/lib/plugins/tabinclude/helper.php:240) in /usr/share/nginx/html/inc/Action/Export.php on line 106
bash_scripting

Bash Scripting

  • Maths
  • Colours
  • Bold and Underlined
  • my_sftp_script
  • apache maxc
  • server stats

bc

You can use bc in bash to do maths calculations with decimals. The man pages can be found:

http://unixhelp.ed.ac.uk/CGI/man-cgi?bc+1

Example Calulation

bc -l <<< "20 / ( 20 + 80) * 100 "
The output looks like:
20.00000000000000000000
-l = --mathlib = Defines the standard math library

Limiting the decimal place

You can then use this with printf to limit the number of decimals shown: \\The example below shows the results to 3 decimal places. You can replace the 3 with what ever value you wish to place

printf "%.3f\n" $(bc -l <<< "20 / ( 20 + 80) * 100 ")

You can also use bc with variables:
bc -l <<< "($used / ( $free + $used) ) * 100 ")

Comparing Decimals with bc

You are able to compare decimal numbers using bc. The output will return 1 or 0 depending on the results. You can then use this with an if statment -le / -ge etc

1.4 '<' 1.3 | bc -l

example= $(echo 1.4 '<' 1.3 | bc -l)
if [ $example-eq 0 ]; then
elif [ $example-eq 1  ]; then
fi