LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2015, 10:45 PM   #1
hounslow
LQ Newbie
 
Registered: Nov 2015
Posts: 1

Rep: Reputation: Disabled
Bash script


I am new to Linux and programming in general. I was wondering if anyone would be willing to show me how to write a script to convert miles per hour to feet per second.
 
Old 11-23-2015, 11:22 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

Just think about the problem logically...

1. Write down the math equation which includes the input variable (mph) and produces the output value (fps).
2. Learn how to use the facilities of your favored scripting language to express that equation.
3. Put in the input value (mph), run the script and get the output value.

So first, since this is a simple units conversion the math will look something like this...

Code:
FPS = k X MPH
   where k is a proportionality constant - the math is left to you...
So it really involves a single multiplication operation.

In a shell script multiplication is indicated by a * character.

Variable values are assigned by simply setting a name equal to a value.

Writing something to the output is very easy.

So, overall a simple script might be something like this...

Code:
#!/bin/bash

MPH=60
k=enter the value from the math...
FPS=0

#Now do the math in terms recognized by the shell (it will require some context characters not shown here)..

FPS=k*MPH

#And write the results out here... you'll figure out how easily enough!
You can find the missing pieces here, Bash Beginners Guide. See if you can put it all together and let us know if you get stuck!

Good luck!

Last edited by astrogeek; 11-23-2015 at 11:31 PM. Reason: Removed redundant comment
 
Old 11-24-2015, 01:41 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The only thing I would add to the above is that by default bash will not calculate decimal numbers, so you either need to use a helper, such as bc, or perform calculations to shift the decimal point
whilst working out your answers.

Of course you also have the ability to use other languages such as Perl or Python

Good luck
 
1 members found this post helpful.
Old 11-24-2015, 02:30 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by hounslow View Post
I am new to Linux and programming in general. I was wondering if anyone would be willing to show me how to write a script to convert miles per hour to feet per second.
This is not something that a beginner programmer will be able to do without "severe motivation", so here's some motivation:

Code:
#!/bin/bash 
# traveltime - a program to calculate how long it will
# take to travel a fixed distance 
# syntax: traveltime miles mph 

X60=$(($1 * 60)) 
TOTMINUTES=$(($X60 / $2)) 
HOURS=$(($TOTMINUTES / 60)) 
MINUTES=$(($TOTMINUTES % 60)) 
echo "The trip will take $HOURS hours and $MINUTES minutes"
usage: script <distance> <speed>

Actual usage:
Code:
sh travel 100 60
The trip will take 1 hours and 40 minutes

That should get you started.

References:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://tldp.org/LDP/abs/abs-guide.pdf
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
 
Old 11-30-2015, 09:47 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
To add to the suggestions above, I have some links in my signature for BASH programming, one of them is a blog entry with suggestions as to how to write and debug BASH scripts. I wrote it a long time ago and probably should update it, but the methodologies are still the same regarding how to debug and things to do to assist yourself in accomplishing your goal.

The most common suggestion is to add a line to your script after the #!/bin/sh line at the top of the script:
Code:
set -xv
What this does is enables debug so that when you run your script it shows detailed output about variable assignments, tests their results, and the line numbers as well as statements being executed.
 
Old 12-11-2015, 01:23 PM   #6
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 95

Rep: Reputation: 17
Advanced Bash Scripting Guide

http://www.tldp.org/LDP/abs/html/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration