Let’s do some Bash Math!

Whereas writing your bash scripts, you’ll typically end up wanting to determine the results of an arithmetic calculation to find out a remaining disk house, file sizes, password expiry dates, variety of hosts, community bandwidth, and so on.

On this chapter of the bash newbie sequence, you’ll study to make use of bash operators to hold out varied arithmetic calculations.

To refresh your reminiscence, listed below are the arithmetic operators in bash:

Operator | Description |
---|---|

+ | Addition |

– | Substraction |

* | Multiplication |

/ | Integer division (with out decimal numbers) |

% | Modulus division (offers solely the rest) |

** | exponentiation (x to the facility y) |

## Performing addition and subtraction in bash scripts

Let’s create a bash script named addition.sh that can merely add two file sizes (in bytes) and show the output.

You should be conversant in arguments in bash scripts by now. I do hope you’re additionally conversant in the lower and du instructions.

The du command offers you the scale of the file alongside . The output has each the file measurement and the file identify. That is the place lower command is used to extract the primary column (i.e. the file measurement) from the output. Output of the du command is handed to chop command utilizing pipe redirection.

This is the script:

#!/bin/bash

fs1=$(du -b $1 | lower -f1)

fs2=$(du -b $2 | lower -f1)

echo “File measurement of $1 is: $fs1”

echo “File measurement of $2 is: $fs2”

complete=$(($fs1 + $fs2))

echo “Whole measurement is: $complete”

Discover that you’ll cross the 2 filenames as arguments to the script. For instance, right here I run the script and cross the 2 recordsdata /and so on/passwd and /and so on/group as arguments:

[email protected]:~/scripts$ ./addition.sh /and so on/passwd /and so on/group

File measurement of /and so on/passwd is: 2795

File measurement of /and so on/group is: 1065

Whole measurement is: 3860

A very powerful line within the addition.sh script is:

complete=$(($fs1 + $fs2))

The place you used the + operator so as to add the 2 numbers $fs1 and $fs2. Discover additionally that to judge any arithmetic expression you must enclose between double parenthesis as follows:

$((arithmetic-expression))

You can too use the minus operator (-) to for subtraction. For instance, the worth of the sub variable within the following assertion will end result to seven:

sub=$((10-3))

## Performing multiplication and division in bash scripts

Let’s create a bash script named giga2mega.sh that can convert Gigabytes (GB) to Megabytes (MB):

#!/bin/bash

GIGA=$1

MEGA=$(($GIGA * 1024))

echo “$GIGA GB is the same as $MEGA MB”

Now let’s run the script to learn the way many Megabytes are there in 4 Gigabytes:

[email protected]:~/scripts$ ./giga2mega.sh 4

Four GB is the same as 4096 MB

Right here I used the multiplication

MEGA=$(($GIGA * 1024))

It’s simple so as to add extra performance to this script to transform Gigabytes (GB) to Kilobytes (KB):

KILO=$(($GIGA * 1024 * 1024))

I’ll allow you to convert Gigabytes to bytes as a observe train!

You can too use the division operator (/) to divide two numbers. For instance, the worth of the div variable within the following assertion will consider to 5:

div=$((20 / 4))

Discover that that is integer division and so all fractions are misplaced. For example, in case you divide 5 by 2, you’re going to get 2 which is wrong, in fact:

[email protected]:~/scripts$ div=$((5 / 2))

[email protected]:~/scripts$ echo $div

2

To get a decimal output; you can also make use of the bc command. For instance, to divide 5 by 2 with the bc command, you should use the next assertion:

echo “5/2” | bc -l

2.50000000000000000000

Discover that you should use different operators as properly with the bc command every time you’re coping with decimal numbers:

## Utilizing energy and the rest (modulo)

Let’s create an influence calculator! I’m going to create a script named energy.sh that can settle for two numbers a and b (as arguments) and it’ll show the results of a raised to the facility of b:

#!/bin/bash

a=$1

b=$2

end result=$((a**b))

echo “$1^$2=$end result”

Discover that I exploit the exponentiation operator

to calculate the results of a raised to the facility of b.

Let’s do a couple of runs of the script to be sure that it yields the right solutions:

[email protected]:~/scripts$ ./energy.sh 2 3

2^3=8

[email protected]:~/scripts$ ./energy.sh Three 2

3^2=9

[email protected]:~/scripts$ ./energy.sh 5 2

5^2=25

[email protected]:~/scripts$ ./energy.sh Four 2

4^2=16

You can too use the modulo operator (%) to calculate integer remainders. For example, the worth of the rem variable within the following assertion will consider to 2:

rem=$((17%5))

The rest right here is 2 as a result of 5 goes into 17 thrice, and two stays!

## Follow Time: Make a Diploma Converter bash script

Let’s wrap up this tutorial by making a script named c2f.sh that can convert Celsius levels to Fahrenheit levels utilizing the equation beneath:

F = C x (9/5) + 32

This will likely be a superb train so that you can attempt the brand new stuff you simply discovered on this bash tutorial.

This is an answer (there may very well be a number of methods to realize the identical end result):

#!/bin/bash

C=$1

F=$(echo “scale=2; $C * (9/5) + 32” | bc -l)

echo “$C levels Celsius is the same as $F levels Fahrenheit.”

I used the bc command as a result of we’re coping with decimals and I additionally used “scale=2” to show the output in two decimal factors.

Let’s do a couple of runs of the script to ensure it’s outputting the right outcomes:

[email protected]:~/scripts$ ./c2f.sh 2

2 levels Celsius is the same as 35.60 levels Fahrenheit.

[email protected]:~/scripts$ ./c2f.sh -3

-Three levels Celsius is the same as 26.60 levels Fahrenheit.

[email protected]:~/scripts$ ./c2f.sh 27

27 levels Celsius is the same as 80.60 levels Fahrenheit.

Good! This brings us to the tip of this tutorial. I hope you could have loved performing some math with bash and keep tuned for subsequent tutorial within the bash newbie sequence as you’ll discover ways to manipulate strings!

**shell script arithmetic operations with variables**,**write the command to perform all the arithmetic operations using basic calculator in unix**,**shell script to perform arithmetic operations using case**,**bash arithmetic float**,**logical operators in shell script example**,**multiple arithmetic operations in shell script**,**awk script to perform simple arithmetic operations**,**multiplication in shell script**