Thursday, 26 September 2013

Basic Linux Shell Scripting Language - Arithmetic Operations


This is the third article on Basic Linux Shell Scripting Language. Before this article, I had published two articles on the said topic, which are:

    I would recommend that, before you go through this article directly, please read this article so that it would become easier for you to understand.



In this article on Basic Linux Shell Scripting Language, we will be dealing with Mathematics. I will try to cover how basic operations in Mathematics are implemented in Shell Scripting along with some scripts which incorporate their uses. Most of these scripts are self explanatory.

Here we go !

1. Addition of Two Numbers

Here, we will need two variables, say x and y, to store two numbers and a third variable 'sum' to store their addition. Create a file 'Add.sh' , make it executable with chmod command and run it.

#!/bin/bash
echo “Enter First Number: ” 
read x 
echo “Enter Second Number: ” 
read y 
sum=$(expr "$x" + "$y") 
echo $x + $y = $sum
Output:

2. Subtraction of Two Numbers

This script 'Sub.sh' will be very much similar to the Addition script. In this script also, we will need two variables to store two inputs and the third variable 'diff' to hold the result 'x-y'.

#!/bin/bash
echo “Enter First Number: ” 
read x 
echo “Enter Second Number: ” 
read y 
diff=$(expr "$x" - "$y") 
echo $x - $y = $diff
Output:


3. Multiplication of Two Numbers

We call this script as 'Mul.sh' and we will use the variable 'product' to store the result 'x * y'. Just remember to escape the '*' here.

#!/bin/bash
echo “Enter First Number: ” 
read x 
echo “Enter Second Number: ” 
read y 
product=$(expr "$x" \* "$y") 
echo "$x * $y = $product "
Output:

4. Division of Two Numbers

Lets now create a file 'Div.sh', we will use the variable 'div' to store the result 'x / y'. Again, we need to escape '/' in the script.

#!/bin/bash
echo “Enter First Number: ” 
read x 
echo “Enter Second Number: ” 
read y 
div=$(expr "$x" / "$y") 
echo "$x / $y = $div "
Output:

5. Multiplication Table of a Number

With the knowledge of basic mathematical operations, time for a Level Up! This time, we will write a script that will print multiplication table of any number. For this, we need a For loop and seq command. seq command will just generate sequence of numbers (1 to 10 in this case).

#!/bin/bash
echo “Enter the number: ” 
read num 

for i in $(seq 1 10)
do
    j=$(expr $i \* $num) 
    echo $j
done
Output:

6. Check Whether the Number is Even or Odd

This time, we will make use of a 'Modulo' operator and one 'If-then-else' statement.

#!/bin/bash
echo “Enter the number: ” 
read num 

i=$(expr $num % 2)

if [ i -eq 0 ]
then
    echo "$num is Even"
else
    echo "$num is Odd"
fi
Output:

7. Find the Factorial of a Number

Now, let us create a script which calculates factorial of a given number using a 'while' loop.

#!/bin/bash
echo “Enter the number: ” 
read num 
fact=1

while [ $num -gt 0 ]
do
    fact=$(expr $fact \* $num)
    num=$(expr $num - 1)
done

echo $fact
Output:

8. Determine Whether a Number is Prime or Not

This script will need both the loop statement and conditional statement.

#!/bin/bash
echo “Enter the number: ” 
read num 
c=1

if [ $num -eq 1 ]
then
    echo "Its Neither Prime nor Composite..!"
else
    i=2
    while [ $i -le $num ]
    do
        j=$(expr $num % $i)
        if [ j -eq 0 ]
        then
            c=$(expr $c + 1)
        fi
    i=$(expr $i + 1)
    done
    
    if [ c -eq 2 ]
    then
        echo "Yeah! Its Prime!"
    else
        echo "No! Its not Prime!"
    fi
fi

Output:

That's all! In this article, I tried to cover how basic mathematical operations are implemented in Shell Scripting along with some scripts which were based on those mathematical operations. For any feedback, please use the comment section below and stay tuned for some more articles on Basic Linux Shell Scripting Language.

2 comments:

  1. Hi, it seems to me that the function "expr" does not work if "x" and "y" are decimal numbers... does it only work for integers?

    ReplyDelete
  2. That's right. Using "expr", you can perform operations only on integers. In case you have to perform operations on decimals, you cal use "bc".

    ReplyDelete

    Total Pageviews