Linux

User input and arithmetic operation in Shell scripting

Taking input is one of the easiest task in shell scripting. Until now we have not been interactive enough.Now that we have basic understanding of shall scripting . It's time to take a step forward.Now we are going to make our script user friendly by interacting with them.

User or keyboard input in Shell Scripting

read command is used to take input.Insert below script into your text editor save and make executable.

 #!/bin/bash

#Keyboard input in shell scripting 

echo "Enter your name"
read name
echo "Good day MR $name"
 
  1. First Line is known as shebang.It is absolute path to the bash interpreter.
  2. Second Line is comment.Anything that begins with # is a comment shell ignores this line.
  3. Now most important is 4rth line of the code read is an inbuilt command in linux.it asks users to enter something from your keyboard. With read we have to supply a variable to store the input we receive from keyboard.
  4. Note that when you print a variable on output screen you must add $ symbol before the variable.

Arithmetic operations with Shell Scripting

Arithmetic operations are very different to the other languages like c, c++,java and python in shell scripting. Look at the following example.

num1=23
num2=56
Now let's say you want to add both number.
echo $num1+$num2
This is not a valid statement.
Shell will treat the above expression as the simple statement.So we have tell shell to treat this as a mathematical expression.This can be done with a command expr. We must put expr before every mathematical expression.

There are couple of ways to perform arithmetic operations in bash scripting.There are few inbuilt functions in bash scripting to perform arithmetic operations:

Arithmetic with expr in bash script

Copy and paste the following script and make it executable and run

#!/bin/bash
#Arithmetic  operations in bash
num1=134
num2=33
add=`expr $num1 + $num2`
rem=`expr $num1 % $num2 `
div=`expr $num1 / $num2`
multiplication=`expr $num1 \* $num2` 
echo "addition is  : $add"
echo "Remainder   is: $rem"
echo "Multiplication is: $multiplication"
echo "Division is $div"

Above script will be expanded successfully.

Rules to use expr

  1. expr : Is an inbuilt function it tells bash evaluate expressions as the mathematical expressions.
  2. As you can see from the above scrip that we have use reverse quote ` before expr and at the end of expression.
  3. There should be space between operator and variable/value.23+23 or num1+num2 is not correct. 23 + 23 or num1 + num2 is a valid expression.
  4. You cant use * symbol for multiplication because * is wild card operator in linux.So you have eliminate wild card by escaping out with \.

More examples on expr

It can also print the value of expression to standard output.You dont have to use echo function.Just type the following in the terminal for quick calculations.It gives output straight away:

expr 5 + 4
expr 5 - 4
expr 5 \* 4
expr 5 % 4
 

Arithmetic with double parenthesis in bash

Another way of evaluating expressions in shell scripting placing your variables or values between double parenthesis.This is probably easier way to do math in bash.

Copy and paste the following script and run.

#!/bin/bash
#Arithmetic  operations in bash with double parenthesis format

num1=237
num2=45
div=$((num1 / num2))
add=$((num1 + num2))
multi=$((num1 * num2))
remainder=$((num1 % num2))
echo "addition is $add"
echo "division is $div"

echo "multiplication is $multi"

echo "remainder is $remainder"

Everything in the script is very self explanatory.Nothing fancy just take care of double parenthesis and and put $ sign before expression.

User friendly Calculator

Now let's make a bash script that will ask users for input .

Copy and run the following script and enjoy.

 
 #!/bin/bash
#Arithmetic  operations in bash with double parenthesis format

echo "Enter first integer"
read num1
echo "enter second integer"
read num2
add=$((num1+num2))
multi=$((num1*num2))
div=$((num1/num2))
remain=$((num1%num2))

echo "addition is $add"
echo "Division is $div"
echo "remainder is $remain"
echo "multiplication is $multi"