[SOLVED] shell script to convert Celsius to Fahrenheit
UbuntuThis forum is for the discussion of Ubuntu Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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).
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
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?
- ?
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"
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).
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.