Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] shell script to convert Celsius to Fahrenheit
 Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices

02-14-2012, 05:40 AM   #1
p3aul
Member

Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 116

Rep:
shell script to convert Celsius to Fahrenheit

I need a small script to convert Celsius to Fahrenheit. I have this one but it terminates after input and doesn't show the result. If it works at all I can't tell. Can someone correct it for me or give me one that will work?

Quote:
 echo "*** Converting between the different temperature scales ***" echo "1. Convert Celsius temperature into Fahrenheit" echo "2. Convert Fahrenheit temperatures into Celsius" echo -n "Select your choice (1-2) : " read choice if [ \$choice -eq 1 ] then echo -n "Enter temperature (C) : " read tc # formula Tf=(9/5)*Tc+32 tf=\$(echo "scale=2;((9/5) * \$tc) + 32" |bc) echo "\$tc C = \$tf F" elif [ \$choice -eq 2 ] then echo -n "Enter temperature (F) : " read tf # formula Tc=(5/9)*(Tf-32) tc=\$(echo "scale=2;(5/9)*(\$tf-32)"|bc) echo "\$tf = \$tc" else echo "Please select 1 or 2 only" exit 1 fi
Thanks,
Paul

Last edited by p3aul; 02-14-2012 at 05:48 AM. Reason: forgot to show the script!

 02-14-2012, 06:02 AM #2 druuna LQ Veteran   Registered: Sep 2003 Posts: 10,532 Blog Entries: 7 Rep: Hi, What is the problem you're having with the above script? Works nicely on my side: Code: ```\$ ./temperature.converter.sh *** Converting between the different temperature scales *** 1. Convert Celsius temperature into Fahrenheit 2. Convert Fahrenheit temperatures into Celsius Select your choice (1-2) : 1 Enter temperature (C) : 10 10 C = 50.00 F \$ ./temperature.converter.sh *** Converting between the different temperature scales *** 1. Convert Celsius temperature into Fahrenheit 2. Convert Fahrenheit temperatures into Celsius Select your choice (1-2) : 2 Enter temperature (F) : 50 50 = 9.90``` That is with and without a proper hash-bang (#!/bin/bash). I would change this line Code: `echo "\$tf = \$tc"` to Code: `echo "\$tf F = \$tc C"` for consistency.
 02-14-2012, 06:08 AM #3 p3aul Member   Registered: Jul 2011 Distribution: Ubuntu 10.04 Posts: 116 Original Poster Rep: The problem is my terminal window closes after I input my temp. it pauses to accept my input to enter the value for temp but then terminates and the terminal window closes
02-14-2012, 06:15 AM   #4
druuna
LQ Veteran

Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep:
Hi,
Quote:
 Originally Posted by p3aul The problem is my terminal window closes after I input my temp. it pauses to accept my input to enter the value for temp but then terminates and the terminal window closes
How exactly are you trying to execute this script?

- Do you open a terminal first and then execute it?
- Did you make a shortcut to the script and (double)click on that?
- ?

02-14-2012, 06:26 AM   #5
p3aul
Member

Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 116

Original Poster
Rep:
Quote:
 How exactly are you trying to execute this script? - Do you open a terminal first and then execute it? - Did you make a shortcut to the script and (double)click on that? - ?
I've been double clicking on the file, which I named temp.sh a dialog opens asking me whether i want to display or run in Terminal. I choose run in terminal. If I open terminal first and then type temp or temp.sh It comes back with "no such command"

02-14-2012, 06:34 AM   #6
druuna
LQ Veteran

Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep:
Hi,
Quote:
 Originally Posted by p3aul I've been double clicking on the file, which I named temp.sh a dialog opens asking me whether i want to display or run in Terminal. I choose run in terminal.
When the script finishes (last action is printing the outcome) it will close and the output isn't visible any more.

You could add a piece of code that waits for a key press before it exits (the bold part):
Code:
```#!/bin/bash

echo "*** Converting between the different temperature scales ***"
echo "1. Convert Celsius temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "

if [ \$choice -eq 1 ]
then
echo -n "Enter temperature (C) : "
# formula Tf=(9/5)*Tc+32
tf=\$(echo "scale=2;((9/5) * \$tc) + 32" |bc)
echo "\$tc C = \$tf F"
elif [ \$choice -eq 2 ]
then
echo -n "Enter temperature (F) : "
# formula Tc=(5/9)*(Tf-32)
tc=\$(echo "scale=2;(5/9)*(\$tf-32)"|bc)
echo "\$tf F = \$tc C"
else
echo "Please select 1 or 2 only"
exit 1
fi

echo -n "
-->  Press any key to exit "

exit 0```
Quote:
 If I open terminal first and then type temp or temp.sh It comes back with "no such command"
When using the terminal use either the full path to the script or let it start with ./
I.e
Code:
```/full/path/to/temp.sh
# or
./temp.sh```
Do make sure that:
- the first line of the script reads #!/bin/bash
- the script is executable (chmod 750 temp.sh).

Hope this helps.

 02-14-2012, 06:48 AM #7 p3aul Member   Registered: Jul 2011 Distribution: Ubuntu 10.04 Posts: 116 Original Poster Rep: yeah I clicked on properties and chose to execute as program. yes, both answers were what I was looking for! Thanks, Paul
02-14-2012, 06:51 AM   #8
druuna
LQ Veteran

Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep:
Hi,
Quote:
 Originally Posted by p3aul yes, both answers were what I was looking for! Thanks, Paul
You're welcome

BTW: Can you put up the [SOLVED] tag.

 03-09-2017, 04:17 AM #9 frenkiel LQ Newbie   Registered: Dec 2005 Location: Paris Posts: 5 Rep: accuract problem I found this thread some years later, but may-be these remarks can help... 37 Celsius gives 98.60 Fahrenheit 98 Fahrenheit gives 36.63 Celsius. I would also suggest splitting in 2 non-interactive scripts, in order to use then in other scripts - cel2far #!/bin/bash echo "2 k 9 5 / \$1 * 32 + p" | dc - far2cel #!/bin/bash echo "2 k \$1 32 - 5 * 9 / p" | dc

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post investmentbnker75 Programming 13 09-05-2010 01:38 AM 3saul Linux - Software 1 01-02-2006 09:40 AM Anon123 Linux - General 4 06-26-2004 05:53 AM meng_en Linux - General 9 10-15-2002 10:46 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:25 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -