Header AD

How To Accept Inputs and Perform Shell Arithmetic

How To Accept Inputs and Perform Shell Arithmetic

How To Accept Inputs and Perform Shell Arithmetic
How To Accept Inputs and Perform Shell Arithmetic

How To Accept Inputs and Perform Shell Arithmetic

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

How To Accept Inputs

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 the absolute path to the bash interpreter.
  2. Second Line is the comment.Anything that begins with # is a comment shell ignores this line.
  3. Now most important is the 4rth line of the code read is an inbuilt command in Linux. it asks users to enter something from your keyboard. With reading we have to supply a variable to store the input we receive from the 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 numbers.
echo $num1+$num2
This is not a valid statement.
Shell will treat the above expression as the simple statement.So we have to tell the 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 the 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 to evaluate expressions as the mathematical expressions.
  2. As you can see from the above script that we have use reverse quote ` before expr and at the end of an 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 can't use * symbol for multiplication because * is wildcard operator in Linux.So you have eliminated wild card by escaping out with \.

More examples on expr

It can also print the value of the expression to standard output.You don't 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 the 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"

So above are the How To Accept Inputs and Perform Shell Arithmetic. Hope you like this article, keep on sharing with others too. Also, share your experience with us in a comment box below.

No comments