Simple math
Posted: Mon 06 Jul 2009, 11:55
Simple math
Sometimes we want to perform simple math on the command line;
add, divide, multiple, subtract - then have the command prompt return after the command.
Introducing the external command: bc
bc is an extremely powerful calculator - powerful enough just using it could become a study in and of itself
Introducing: shift
shift is an internal bash command. It moves the variable string from right to left. Here is an example:
args = these are my arguments
$1 : these
$2 : are
$3 : my
$4 : arguments
The 'shift' instruction makes this change
args = are my arguments
$1 : are
$2 : my
$3 : arguments
Note how $1 disappeared and the other arguments are re-designated right to left.
You've been introduced to; the case statement, shift, bc, | and echo
Curly braces {}
{ : begin
} : end
In this example, the curly braces are way overdone, we use them to make sure bash doesn't misinterpret where a variable ends. The $ says where it starts, but where does it end?
Is the variable $1 or is it $1- ?
The curly braces define ${1} as exclusive from any character immediately following.
The wildcard *
In this case statement, anything which starts with the letter a is a match because of the wildcard *
a, add, and advertise all mean the same thing.
If our first argument does not start with a,d,m,s ; the * at the end does match, and prints out a message and returns our command prompt.
This simple, even simplistic script does what it's made to do and even handles floating point math.
We use $1 to determine if we want to add, divide, subtract or multiple. I use the shift command mostly for demonstration. We could also not shift and use arguments $2 and $3 for processing the math. Makes sense? I hope.
Usage examples
math a 8 81
math d 124 2
math s 9 3
math m 1024 1024
Running totals:
double click the result and paste it into your next calculation
The purpose is . . .
. . .making things easier when we want simple math
Mandatory Assignment
Make a new utility called math by using our easy copy and paste method; (newsh)
Practice with it some
~
Chapter 32 - Simple math
Sometimes we want to perform simple math on the command line;
add, divide, multiple, subtract - then have the command prompt return after the command.
Introducing the external command: bc
bc is an extremely powerful calculator - powerful enough just using it could become a study in and of itself
Introducing: shift
shift is an internal bash command. It moves the variable string from right to left. Here is an example:
args = these are my arguments
$1 : these
$2 : are
$3 : my
$4 : arguments
The 'shift' instruction makes this change
args = are my arguments
$1 : are
$2 : my
$3 : arguments
Note how $1 disappeared and the other arguments are re-designated right to left.
Code: Select all
case ${1} in
a*) shift ; echo ${1}+${2} | bc -l ;;
d*) shift ; echo ${1}/${2} | bc -l ;;
m*) shift ; echo ${1}\*${2} | bc -l ;;
s*) shift ; echo ${1}-${2} | bc -l ;;
*) echo "a*dd | d*iv | m*ul | s*ub" ;;
esac
Curly braces {}
{ : begin
} : end
In this example, the curly braces are way overdone, we use them to make sure bash doesn't misinterpret where a variable ends. The $ says where it starts, but where does it end?
Is the variable $1 or is it $1- ?
The curly braces define ${1} as exclusive from any character immediately following.
The wildcard *
In this case statement, anything which starts with the letter a is a match because of the wildcard *
a, add, and advertise all mean the same thing.
If our first argument does not start with a,d,m,s ; the * at the end does match, and prints out a message and returns our command prompt.
This simple, even simplistic script does what it's made to do and even handles floating point math.
We use $1 to determine if we want to add, divide, subtract or multiple. I use the shift command mostly for demonstration. We could also not shift and use arguments $2 and $3 for processing the math. Makes sense? I hope.
Usage examples
math a 8 81
math d 124 2
math s 9 3
math m 1024 1024
Running totals:
double click the result and paste it into your next calculation
The purpose is . . .
. . .making things easier when we want simple math
Mandatory Assignment
Make a new utility called math by using our easy copy and paste method; (newsh)
Practice with it some
~
Chapter 32 - Simple math