• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Example Bash Script For Arithmetic Math Calculation with Input Value

By Abhishek Ghosh January 25, 2015 6:58 pm Updated on January 25, 2015

Example Bash Script For Arithmetic Math Calculation with Input Value

Advertisement

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 :

Vim
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

Bash Script For Arithmetic Math Calculation with Input Value

So the Formula is :

Advertisement

---

Vim
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 :

Vim
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 :

Vim
1
$(((4 * 2)+((4 - 2)*2)+((4 - 2)*2)))

If you want to directly execute it on command line, you should copy paste this :

Vim
1
echo "$(((4 * 2)+((4 - 2)*2)+((4 - 2)*2)))"

In this case, we have two equations, so we can write in this way :

Vim
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 :

Vim
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 :

Vim
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

This Article Has Been Shared 822 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Orthopaedic Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Example Bash Script For Arithmetic Math Calculation with Input Value

  • Bash Completion Tutorial

    Here is a Bash Completion Tutorial So That a Command Line User Can Either Use the Existing Bash Completion Examples or Create Own Bash Script.

  • Mainframe Systems and Terminal Emulation : Best Practices

    Mainframe Systems and Terminal Emulation – your computer support consultants might be talking about terminal emulation.

  • Moving to Cloud : Is It Safe For Your Business ?

    Moving to Cloud is not simple outsourcing. It means the possibility for the companies to use online applications in all areas, needs careful analysis to adapt.

  • How to Use tmux on Mac

    tmux is used to multiplex several virtual consoles to access multiple terminal sessions inside a single terminal window or remote terminal.

  • Online Certificate Status Protocol (OSCP)

    Online Certificate Status Protocol (OSCP) is a RFC 6960 standard, it is a method to determine the revocation status of a digital certificate.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

You can subscribe to our Free Once a Day, Regular Newsletter by clicking the subscribe button below.

Click To Subscribe

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (21K Followers)
  • Twitter (5.3k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • What is Standard Software February 28, 2021
  • WordPress Link to text Fragment February 27, 2021
  • How to Protect IP Cameras From Hackers February 25, 2021
  • 6 Sectors That Have Undergone Revamps in Digital Landscape February 24, 2021
  • What You Can Control with a Smartwatch and ESP32 February 23, 2021

 

About This Article

Cite this article as: Abhishek Ghosh, "Example Bash Script For Arithmetic Math Calculation with Input Value," in The Customize Windows, January 25, 2015, March 2, 2021, https://thecustomizewindows.com/2015/01/example-bash-script-arithmetic-math-calculation-input-value/.

Source:The Customize Windows, JiMA.in

 

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Cookie Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2021 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy