• 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 644 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, 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

Get new posts by email:

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 (24.3K Followers)
  • Twitter (5.8k 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

  • The Importance of Voice and Style in Essay Writing April 1, 2023
  • What Online Casinos Have No Deposit Bonus in Australia March 30, 2023
  • Four Foolproof Tips To Never Run Out Of Blog Ideas For Your Website March 28, 2023
  • The Interactive Entertainment Serving as a Tech Proving Ground March 28, 2023
  • Is it Good to Run Apache Web server and MySQL Database on Separate Cloud Servers? March 27, 2023

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, April 2, 2023, https://thecustomizewindows.com/2015/01/example-bash-script-arithmetic-math-calculation-input-value/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

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

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

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT