LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   shell script to convert Celsius to Fahrenheit (https://www.linuxquestions.org/questions/ubuntu-63/shell-script-to-convert-celsius-to-fahrenheit-929261/)

p3aul 02-14-2012 05:40 AM

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

druuna 02-14-2012 06:02 AM

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.

p3aul 02-14-2012 06:08 AM

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

druuna 02-14-2012 06:15 AM

Hi,
Quote:

Originally Posted by p3aul (Post 4601967)
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?
- ?

p3aul 02-14-2012 06:26 AM

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"

druuna 02-14-2012 06:34 AM

Hi,
Quote:

Originally Posted by p3aul (Post 4601977)
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.

p3aul 02-14-2012 06:48 AM

yeah I clicked on properties and chose to execute as program.

yes, both answers were what I was looking for!
Thanks,
Paul

druuna 02-14-2012 06:51 AM

Hi,
Quote:

Originally Posted by p3aul (Post 4601994)
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

frenkiel 03-09-2017 04:17 AM

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


All times are GMT -5. The time now is 09:59 PM.