Shell script building blocks – maths addition numbers
Shell script learning starts here. The very first learners in the world start from here and become an expert in writing shell scripts. In this article we are going to see shell script building blocks do little bit of maths addition numbers.
We are going to explain shell script and play with it.
Shell script building blocks – maths addition numbers
First maths addition numbers shell script, is below
#!/bin/bash ## Purpose: Addition of Two Numbers ## Author: Ankam Ravi Kumar ## Date: 6th Sep 2016 ## Website: https://arkit.co.in # START X=5 Y=10 echo "addition of X+Y" echo "X+Y = $X+$Y = $[ X+Y ]" # END
In above script what we are doing is, we specified an X and Y variable values as 5 and 10. X one variable and Y one variable.
in line echo “X+Y = $x+$y = $[ X+Y ]” we are calling ( $X ) X value ( $Y ) Y value. ‘+’ plus symbol means we are doing X+Y addition.
Above script output is
addition of X+Y X+Y = 5+10 = 15
Above script is not flexible for us to calculate specified values truly we can’t specify our own numbers for addition. We will modify the above script a little bit to ask user input and calculate additon based on user given values.
Shell script building blocks – maths addition numbers
Below is the modified script
#!/bin/bash ## Purpose: Addition of Two Numbers ## Author: Ankam Ravi Kumar ## Date: 6th Sep 2016 ## Website: https://arkit.co.in # START echo "addition of X+Y" echo "Enter Any Number" read X echo "Enter One More Number" read Y echo "X+Y = $X+$Y = $[ X+Y ]" # END
In this modified script we are able to see two more lines of code extra then first script, what read X and read Y do is, read is a command which will read the user input and store as variable, whereas our variable values are X and Y.
[root@TechTutorials scripts]# sh addition1.shaddition of X+Y Enter Any Number 13556 Enter One More Number 8900 X+Y = 13556+8900 = 22456
Inbuilt above script as your regular command, Every time you no need to execute the script instead of you can just type as command ‘addition’.
[root@Techtutorials scripts]# chmod 777 addition.sh[root@Techtutorials scripts]# cp addition.sh /usr/bin/addition [root@TechTutorials scripts]# addition addition of X+Y Enter Any Number 789064427 Enter One More Number 34 X+Y = 789064427+34 = 78906446
Conclusion: Using above script we can do any complex numbers addition within a sec of time.
Related Articles
Real Time CPU Utilization Monitoring Shell Script
4 Free PDF books to learn Shell Scripting
Create Multiple users with single shell script
Monitoring your File system Usage
Thanks for your wonderful Support and Encouragement