LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices

Reply
 
Search this Thread
Old 02-14-2012, 05:40 AM   #1
p3aul
Member
 
Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 115

Rep: Reputation: Disabled
Cool 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!
 
Old 02-14-2012, 06:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 02-14-2012, 06:08 AM   #3
p3aul
Member
 
Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 115

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-14-2012, 06:15 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,
Quote:
Originally Posted by p3aul View Post
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?
- ?
 
Old 02-14-2012, 06:26 AM   #5
p3aul
Member
 
Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 115

Original Poster
Rep: Reputation: Disabled
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"
 
Old 02-14-2012, 06:34 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,
Quote:
Originally Posted by p3aul View Post
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) : "
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 F = $tc C"
else
  echo "Please select 1 or 2 only"
  exit 1
fi


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

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.
 
Old 02-14-2012, 06:48 AM   #7
p3aul
Member
 
Registered: Jul 2011
Distribution: Ubuntu 10.04
Posts: 115

Original Poster
Rep: Reputation: Disabled
yeah I clicked on properties and chose to execute as program.

yes, both answers were what I was looking for!
Thanks,
Paul
 
Old 02-14-2012, 06:51 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

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

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Convert python script to shell script investmentbnker75 Programming 13 09-05-2010 01:38 AM
convert shell script to c code 3saul Linux - Software 1 01-02-2006 09:40 AM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM
how to convert a text file into shell script meng_en Linux - General 9 10-15-2002 10:46 PM


All times are GMT -5. The time now is 03:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration