On the previous article we have seen the basic concepts of shell scripting. As a part of basic operator we will see arithmetic operations using shell scripts.

Arithmetic Operators :

    Arithmetic operators are used to perform normal arithmetics/mathematical operations. As like other programs it performs basic mathematic functions. There are 7 arithmetic operators.

    We will write program for every individual operators by passing command line arguments.

    For input we will be using read
   
    And we will use corresponding operators to perform the arithmetic operations.

    We will use echo to print the output.

Addition (+):

    Binary operation used to add two operands.

    Open a file as add.sh

$ vim add.sh

    Paste the below code

Code:

#!/bin/sh

echo "Please enter the numbers:"
read num1
read num2
sum=$(($num1 + $num2))
echo "The sum of $num1 and $num2 is $sum."

    Run the file as below,

jhony@ljlinux:~$ ./add.sh
Please enter the numbers:
10
10
The sum of 10 and 10 is 20.

Subtraction (-):
   
    Binary operation used to subtract two operands.

    Open a file as sub.sh

$ vim sub.sh

    Paste the below code

Code:

#!/bin/sh

echo "Please enter the numbers:"
read num1
read num2
diff=$(($num1 - $num2))
echo "The difference of $num1 and $num2 is $diff."

    Run the file as below,

jhony@ljlinux:~$ ./sub.sh
Please enter the numbers:
10
5
The difference of 10 and 5 is 5.

Multiplication (*):

    Binary operation used to multiply two operands.

    Open a file as mul.sh

$ vim sub.sh

    Paste the below code

Code:

#!/bin/sh

echo "Please enter the numbers:"
read num1
read num2
mul=$(($num1 * $num2))
echo "Multiplication of $num1 and $num2 is $mul."

    Run the file as below,

jhony@ljlinux:~$ ./mul.sh
Please enter the numbers:
10
5
Multiplication of 10 and 5 is 50.

Division (/) :   

    Binary operation used to divide two operands.

    Open a file as div.sh

$ vim div.sh

    Paste the below code

Code:

#!/bin/sh

echo "Please enter the numbers:"
read num1
read num2
div=$(($num1 / $num2))
echo "Divident of $num1 by $num2 is $div."

    Run the file as below,

jhony@ljlinux:~$ ./mul.sh
Please enter the numbers:
10
5
Divident of 10 by 5 is 2.

Modulus (%) :   

    Binary operation used to find remainder of two operands.

    Open a file as div.sh

$ vim div.sh

    Paste the below code

Code:

#!/bin/sh

echo "Please enter the numbers:"
read num1
read num2
mod=$(($num1 % $num2))
echo "The remainder of $num1 by $num2 is $mod."

    Run the file as below,

jhony@ljlinux:~$ ./mul.sh
Please enter the numbers:
10
3
The remainder of 10 by 3 is 1.

Increment Operator (++) :

    Unary operator used to increase the value of operand by one.

    Open a file as inc.sh

$ vim inc.sh

    Paste the below code

Code:

#!/bin/sh

echo "Enter the number:"
read num1
out=$(($num1 + 1))
echo "The increment of $num1 is $out."

    Run the file as below,

jhony@ljlinux:~$ ./inc.sh
Enter the number:
5
The increment of 5 is 6.

Decrement Operator (–) :

    Unary operator used to decrease the value of a operand by one

    Open a file as dec.sh

$ vim dec.sh

    Paste the below code

Code:

#!/bin/sh

echo "Enter the number:"
read num1
out=$(($num1 - 1))
echo "The increment of $num1 is $out."

    Run the file as below,

jhony@ljlinux:~$ ./dec.sh
Enter the number:
5
The decrement of 5 is 4.

    Thats it for Arithmetic operations, Feel free to ask if you have any questions.

Comments

  1. Be the first to add a comment.