Here is an Example Bash Script For Arithmetic Math Calculation with Input Value. With Simple Logical Sets, in Bash, we do run complex stuffs. This was made for answering a Quora’s question. May be, someone looking for learning bash scripting. We wrote about writing bash script before and about unix shell.
Example Bash Script For Arithmetic Math Calculation with Input Value : The Problem
Typically on various social networks, you’ll see that, people posts diagram of Pyramids made of balls. Usually they have four layers. Balls can be arranged in two ways – hollow inside and full solid. For per layer count of balls except the top one (base layer’s number will be 04 if it is a 4 layered pyramid) as for solid, will be :
1 2 3 4 5 6 | Base = (4 * 2 ) + ( 2 * 2 ) = 12 Layer 1 = (3 * 2) + ( 1 * 2 ) = 8 Layer 2 = ( 2 * 2 ) + (0 * 2 ) = 4 Layer 3 = constant = 1 ------------------------------------ Total = 12 + 8 + 4 +1 = 25 |
So the Formula is :
---
1 | [ {number of the layer from top x 2} + {(number of the layer from top - 2) x 2} ] |
Total = Sum of Formula’s output for all layers except the top + 1 for the top
If it is not hollow :
1 | [ { number of the layer from top x 2} + {(number of the layer from top - 2) x 2} + {(number of the layer from top -2) x 2} ] |
Calculation is same for both the ways.
Balls required for constructing the Layer 4 (base) for a solid one = [ {4 x 2} + {(4 – 2) x 2} + {(4 -2) x 2} ] = 8 + 4 + 4 = 16. Balls required for constructing the Layer 3 for a solid one = [ {3 x 2} + {(3 – 2) x 2} + {(3 -2) x 2} ] = 6 + 2 + 2 = 10. Balls required for constructing the Layer 2 for a solid one = [ {2 x 2} + {(2 – 2) x 2} + {(2 -2) x 2} ] = 4 + 0 + 0 = 4. Total = 16+10+4 + 1 = 31
Sir Newton wrote the thing. I have not discovered. It is related to structural stability. You can not keep it, if there is no gravity, you can not keep with magnet bars inside with opposite poles facing in the same place.
This is an example where an input value is needed and it should be passed and the calculation will be done. With non-math things, we can use [Y], [N] and catch both Y, y and N, n set to pass via if do it, else exit logic.
Example Bash Script For Arithmetic Math Calculation with Input Value
For the solid’s layer 4, we can write in this way :
1 | $(((4 * 2)+((4 - 2)*2)+((4 - 2)*2))) |
If you want to directly execute it on command line, you should copy paste this :
1 | echo "$(((4 * 2)+((4 - 2)*2)+((4 - 2)*2)))" |
In this case, we have two equations, so we can write in this way :
1 2 3 4 5 6 | #!/bin/bash echo "What is the number of Layer counted from the Top?:" read input echo "Then the $input th layer will have ::" echo "$(((input * 2)+((input - 2)*2)+((input - 2)*2))) balls, if it is solid," echo "and $(((input*2)+((input - 2)*2))) balls, if it is hollow!" |
$input th layer
means, it will read like “7 th layer”. But the digits need a bit color :
1 2 3 4 5 6 | #!/bin/bash echo "What is the number of Layer counted from the Top?:" read input echo "Then the $input th layer will have ::" echo "\x1B[36m$(((input * 2)+((input - 2)*2)+((input - 2)*2)))\x1B[0m balls, if it is solid," echo "and \x1B[36m$(((input*2)+((input - 2)*2)))\x1B[0m balls, if it is hollow!" |
You need to save it as .sh
file and make it executable and then run it. I have ready to use solution :
1 2 3 | wget https://gist.githubusercontent.com/AbhishekGhosh/847323ec7d35c678fa26/raw/de99d4ea7837df65703b6777d2a59981140509aa/pyramid-basic.sh chmod +x pyramid-basic.sh sh pyramid-basic.sh |
The output has been shown in the screenshot.
Tagged With bash math , calculation in bash , bash calculation , math in bash script , bash math examples , doing math in bash , python math calculations in a bash script , bash echo calculation , bash arithmetic , bash math elevation