Articles

SHELL SCRIPT TO DIVIDE TWO NUMBERS

SHELL SCRIPT TO DIVIDE TWO NUMBERS


In this chapter of shell scripting, we are going to learn:

  • shell script to divide two numbers
  • understand the issue in division process
  • rewrite the script to correct the issue

Lets see the steps to perform division in shell script.

 

STEPS SHELL SCRIPT TO DIVIDE TWO NUMBERS

Please follow below steps to divide two numbers in shell script:

  1. Create a shell script with name say, tihDivision.sh
  2. Use echo to provide message - "Division task in progress" [This step is optional. We have just included to let user know what is happening]
  3. Read first input from user
  4. Read second input from user
  5. We can simply calculate the division using $ and (). Lets see the script.

 

SHELL SCRIPT CODE

Given below is the script file containing code for division in shell script:

file name: tihDivision.sh

echo "Division in progress..."
echo "Enter first number"
read number1
echo "Enter second number"
read number2

divResult=$(($number1/$number2))
echo "result " $divResult

OUTPUT

$ ./tihDivision.sh
Division in progress...
Enter first number
10
Enter second number
5
result  2
 
./tihDivision.sh is command used to execute the shell script with name tihDivision.sh
I have executed the command being at tih_linux directory where the file is present.
Given below is the screenshot of the same code - shell script for division of two numbers

shell script to divide two numbers

This is the shell script where user is asked to enter the input from console after executing the script.

 

Lets see another script for doing similar task to write shell script to divide two numbers by providing numbers (inputs) as parameter.


SHELL SCRIPT TO DIVIDE TWO NUMBERS BY ACCEPTING INPUT AS PARAMETERS

We have modified above script to accept two numbers as parameter while executing the shell script file - tihDivision.sh

echo "Division in progress..."
number1=$1
echo "first number is " $number1
number2=$2
echo "second number is " $number2

divResult=$(($number1/$number2))
echo "result " $divResult

OUTPUT

$ ./tihDivision.sh 9 3
Division in progress...
first number is  9
second number is  3
result  3
 

Did you notice the parameters?

See the blue line above ./tihDivision.sh 9 3

Here, we are saying that please execute shell script having name tihDivision.sh that has first parameter as 9 and second parameter as 3.

number1=$1 here $1 will be replaced by first parameter 9

number2=$2 here $2 will be replaced by second parameter 3

If you compare this way of executing the code with the first approach you will notice that in first script we didn't provide any parameter and asked user to input numbers after executing the script. But second approach provides both numbers as parameter. Please, go through both the script again if you missed it. 

 

Given below is the screenshot for division in shell script that accepts arguments:

Code snippet for above code with output is below:

SHELL SCRIPT TO DIVIDE TWO NUMBERS with arguments

Output

 

ISSUE IN ABOVE SCRIPTS??

There is minor issue in above scripts division process.

This division approach will work only when second number completely divides first number.

For example, 6/2=3 or 10/5=2 or 16/4=4 and so on.

 

If you pass number1=9 and number2=6 the lets see what's the result?

result=1 //this is the issue. It doesn't give you output to correct decimal places.

The correct output for above division should be something like 1.50. Isn't it?

shell divide number

 

You can further try for more such examples like 5/3 or 22/5 or 18/8 and so on. You will see it will not return to the correct decimal places. 

 

So can't we get the result upto correct decimal place?

Definitely we can. And the next shell script will so you the same.


SHELL SCRIPT TO DIVIDE TWO NUMBERS UPTO DECIMAL PLACES

We have modified the earlier script and now it also prints result upto decimal places.

 

shell script for division of two numbers is given below:

echo "Division in progress..."
number1=$1
echo "first number is " $number1
number2=$2
echo "second number is " $number2


divResult=$(($number1/$number2))
echo "result " $divResult

echo ""

echo "below prints avg upto decimal space"
echo $number1 $number2 | awk '{print $1/$2}'

OUTPUT

$ ./tihDivision.sh 4 3
Division in progress...
first number is  4
second number is  3
result  1
 
below prints avg upto decimal space
1.33333
 
Look at the last line of the script.
echo $number1 $number2 | awk '{print $1/$2}'
Here awk command receives number1 & number2 as parameter from echo command and then prints the result of division of $1/$2 i.e number1/number2
awk is very useful command and is used for lot of other purposes as well such as printing contents of file with different patterns and matching regular expressions. Don't worry we will learn that as well in upcoming tutorials.
Given below is the screen shot of the above output:

 

Now we have got the result correctly with help of awk. Request you to play around with it and try get different inputs and verify the outputs.

 

We have come to end of this chapter on - shell script for division of two numbers.

Thanks for reading...


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : May 18,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!