Square Root Under BASH Shell


The Linux Shell provides a useful calculator which is bc. It may be short for ‘bash calculator’. The bc takes the following parameters.

1
2
3
4
5
6
7
8
9
root@uploadbeta:~# bc --help
usage: bc [options] [file ...]
  -h  --help         print this usage and exit
  -i  --interactive  force interactive mode
  -l  --mathlib      use the predefined math routines
  -q  --quiet        don't print initial banner
  -s  --standard     non-standard bc constructs are errors
  -w  --warn         warn about non-standard bc constructs
  -v  --version      print version information and exit
root@uploadbeta:~# bc --help
usage: bc [options] [file ...]
  -h  --help         print this usage and exit
  -i  --interactive  force interactive mode
  -l  --mathlib      use the predefined math routines
  -q  --quiet        don't print initial banner
  -s  --standard     non-standard bc constructs are errors
  -w  --warn         warn about non-standard bc constructs
  -v  --version      print version information and exit

Some quick examples of using bash are:

1
2
3
4
5
6
root@uploadbeta:~# echo 'scale=30;sqrt(2)' | bc
1.414213562373095048801688724209
root@uploadbeta:~# echo '355/113.0' | bc
3
root@uploadbeta:~# echo '6^2' | bc
36
root@uploadbeta:~# echo 'scale=30;sqrt(2)' | bc
1.414213562373095048801688724209
root@uploadbeta:~# echo '355/113.0' | bc
3
root@uploadbeta:~# echo '6^2' | bc
36

By default, the bc will be in interactive mode if no input string/file is given.

1
2
3
4
5
6
7
8
9
root@uploadbeta:~# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+2
3
quit
root@uploadbeta:~#
root@uploadbeta:~# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+2
3
quit
root@uploadbeta:~#

Under interactive mode, you type in expressions and results are computed and printed out until the ‘quit’ command is given.

As you already see in the above example, the bc uses sqrt() function to compute the square root, we might wrap this up a little bit in a bash shell.

1
2
3
4
5
6
7
8
#!/bin/bash
 
[ $# -ne 1 ] && {
  echo 'Usage: sqrt number'
  exit 1
} || {
   echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
}
#!/bin/bash

[ $# -ne 1 ] && {
  echo 'Usage: sqrt number'
  exit 1
} || {
   echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
}
1
2
3
4
5
6
root@uploadbeta:~# chmod +x ./sqrt
root@uploadbeta:~# ./sqrt 3
1
root@uploadbeta:~# ./sqrt 3.0
1.7
root@uploadbeta:~# 
root@uploadbeta:~# chmod +x ./sqrt
root@uploadbeta:~# ./sqrt 3
1
root@uploadbeta:~# ./sqrt 3.0
1.7
root@uploadbeta:~# 

We check the number of parameters (shell variable $#), if it is not one, then print the usage (compound command &&) otherwise (compound command ||) prints the sqrt using bc

The echo -e will treat the escape characters and head -2 | tail -1 will disregard the first line (sqrt($1)) and last line (quit) in the printout.

You can also write it this way:

1
2
3
4
5
6
7
8
#!/bin/bash
 
if [ $# -ne 1 ]; then
  echo 'Usage: sqrt number'
  exit 1
else
  echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
fi
#!/bin/bash

if [ $# -ne 1 ]; then
  echo 'Usage: sqrt number'
  exit 1
else
  echo -e "sqrt($1)\nquit\n" | bc -q -i | head -2 | tail -1
fi

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
404 words
Last Post: Getting the Source File Name and Line Number using __FILE__ and __LINE__ compiler constants in C/C++
Next Post: Static Method Called using NULL Pointer in C++

The Permanent URL is: Square Root Under BASH Shell

Leave a Reply