Converting from Centigrade to Fahrenheit and vice versa.

I need to write a script that will take the input from a file and convert the number from centigrade to fahrenheit and vice versa.
This is what I have but it doesn't seem to be correct.
Also the data file has 11 numbers inside of it and the output needs to be listed as so:

Fahrenheit Temperature                                       Centrigade Temperature
-----------------------                                       ---------------------
1                                                                      -17
----------------------                                         --------------------
3                                                                      -13
----------------------                                        ---------------------
89                                                                      31
----------------------                                       -----------------------
etc.
#!/bin/sh
print_dash_line()
{
#sub function to print dashes
echo "_ _ _ _ _ _ _ _ _ _ _ _        _ _ _ _ _ _ _ _ _ _ _ _ "
}
#***** Start of Main Program *****
#***** Assign Variables
input_file=data.file
output_file=ans.file
# formula ft=(9/5)*ct+32
# formula ct=(5/9)*(ft-32)
echo "1. Convert Centigrade temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperature into Centigrade"
echo -n "Select your choice (1-2) : "
read choice
if [ $choice -eq 1 ]
then
   echo -n "Enter Centigrade Temperature : "
   set `cat $input_file`
   echo $1 $2 $3 $4
   read ct
   ft=$(echo "scale=2;((9/5) * $ct) + 32" |bc)
   echo " Centigrade Temperature        Fahrenheit Temperature " > $output_file
   print_dash_line  >> $output_file
   echo >> $output_file
   echo "          $ct                           $ft " >> $output_file
   print_dash_line >> $output_file
elif [ $choice -eq 2 ]
then
   echo -n "Enter Fahrenheit Temperature : "
   set `cat $input_file`
   echo $1 $2 $3 $4
   read ft
   ct=$(echo "scale=2; (5/9)*($ft-32)" |bc)
   echo " Fahrenheit Temperature        Centigrade Temperature " > $output_file
   print_dash_line  >> $output_file
   echo >> $output_file
   echo "          $ft                          $ct " >> $output_file
   print_dash_line >> $output_file
else
    echo "Please select 1 or 2"
    exit l
fi

Do we have to guess what's wrong with it? What error do you get?

I don't like this: set `cat $input_file`
What's input_file used for? (here is not used at all it seems)
Is the goal to use user input from keyboard, or the input file??? What is the format of the input file?

Also, is this homework? There's a special sub-forum for that.

1 Like

It's not technically homework. I found the problem on the net.
The input file looks like this:

1
8
22
43
89
283
120
212
1043
100
287

As far as I can tell the idea is to take the input file or data.file and convert them f2c or c2f. The only input from the keyboard should be 1 or 2 as to choose c2f or f2c.
I'm just trying to get this thing to work.

you want to read from input file line-by-line, the values then. probably you want to convert it both ways, or take from keyboard?

to read file line-by-line is something like this:

while read temp; do
    ...code...
done < "$input_file"
1 Like

Or just do this:

# centigrade to fahrenheit
awk '{ $1 = (5/9)*($1-32) } 1' file

# fahrenheit to celsius
awk {$1= ((9/5)*$1)+32 } 1' file
1 Like

I'm sorry, where would that code go? Would I be ommiting anything from my script? It would be great if you could copy my script and add or remove things as necessary, if that isn't too much trouble. It just helps me to see your thinking faster and better. As it stands when I run the program I have to pick between the 11 numbers which doesn't seem right, it should just run through the input file. The other problem then is that I can only choose one number. I assume that if we can figure out how to fix the having to pick numbers from the keyboard problem the other problem will fix as well.

---------- Post updated 04-29-12 at 12:57 PM ---------- Previous update was 04-28-12 at 10:13 PM ----------

#!/bin/sh
print_dash_line()
{
#sub function to print dashes
echo "_ _ _ _ _ _ _ _ _ _ _ _        _ _ _ _ _ _ _ _ _ _ _ _ "
}
#***** Start of Main Program *****
#***** Assign Variables
input_file=data.file
output_file=ans.file
# formula ft=(9/5)*ct+32
# formula ct=(5/9)*(ft-32)
echo "1. Convert Centigrade temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperature into Centigrade"
echo -n "Select your choice (1-2) : "
read choice
if [ $choice -eq 1 ]
then
   echo -n "Enter Centigrade Temperature : "
   echo " Centigrade Temperature      Fahrenheit Temperature " > $output
  print_dash_line >> $output
while read ct
 do
    ft=$(echo "scale=2;((9/5) * $ct) +32" |bc)
    echo "             $ct                        $ft " >> $output
    print_dash_line >> $output
  done < "$input"
elif [ $choice -eq 2 ]
then
   echo -n "Enter Fahrenheit Temperature : "
   set `cat $input_file`
   echo $1 $2 $3 $4
   read ft
   ct=$(echo "scale=2; (5/9)*($ft-32)" |bc)
   echo " Fahrenheit Temperature        Centigrade Temperature " > $output_file
   print_dash_line  >> $output_file
   echo >> $output_file
   echo "          $ft                          $ct " >> $output_file
   print_dash_line >> $output_file
else
    echo "Please select 1 or 2"
    exit l
fi

I made the changes in red. Now I get a syntax error. Any ideas on your end?

(My post is out of step with your most recent post. However, if you get an error message from post #6 please post the error message verbatim).

At a pure mathematics level, do your multiplies before your divides to avoid a basic loss of accuracy.

ft=212
echo "Formula 1" 
ct=$(echo "scale=2; (5/9)*($ft-32)" |bc)
echo "Farenheit : $ft"
echo "Centigrade: $ct"
echo "Formula 2"
ct=$(echo "scale=2; ((($ft - 32) * 5) / 9)" |bc)
echo "Farenheit : $ft"
echo "Centigrade: $ct"

Formula 1
Farenheit : 212
Centigrade: 99.00
Formula 2
Farenheit : 212
Centigrade: 100.00
1 Like

methyl, you got rid of the syntax error but now the output file only shows -17 in the centigrade column and nothing in the fahrenheit column when I select to go from centigrade to fahrenheit.

@NickNak
No intention to address the syntax error because you have not posted the syntax error. I was addressing the mathematics.

Please post the current version of the script complete with sample input and expected output. I can see a definite conflict between input from the file and and input from the keyboard because as far as Shell is concerned they both come from the same place ... STDIN.

I know you weren't trying to fix the syntax error but somehow just by fixing the math the error went away. At any rate, here is the current code and I really do appreciate the help I have received so far, I feel I am learning this stuff little by little and it is great.

#!/bin/bash
print_dash_line()
{
echo "--------------------     --------------------"
}

input=p3data
output=p3output

echo "1. Convert Centrigade temperature into Fahrenheit temperature."
echo "2. Convert Fahrenheit temperature into Centigrade temperature."
echo -n "Select your choice (1-2): "
read choice;
if [ $choice -eq 1 ]
then
  echo -n "Enter Centrigade Temperature : "
  set `cat $input`
  echo " Centigrade Temperature      Fahrenheit Temperature " > ${output}
  print_dash_line >> ${output}
  while read ct
  do
    ct=$(echo "scale=2; (5/9)*($ft-32)" |bc)
    echo "             $ct                        $ft " >> $output
    print_dash_line >> $output
  done < ${input}
elif
  echo -n "Enter Fahrenheit Temperature : "
  echo " Fahrenheit Temperature      Centigrade Temperature " > ${output}
  print_dash_line >> ${output}
  while read ft
  do
    ft=$(echo "scale=2; ((($ft -32) * 5) / 9)" |bc)
    echo "             $ft                        $ct " >> $output
    print_dash_line >> $output
  done < ${input}
then
    echo "Please select 1 or 2"
    exit l
fi

In case you forgot.

Thank you for taking interest in my correction to the mathematical formula, but did you amend the Farenheit or the Centigrade conversion I wonder? Please check your code thoroughly.

1 Like

The current code I have is in post 10. I am not sure why I can't get the data file numbers to come up with the calculated numbers in the output file. The calculation also only works f2c. C2f only gives me 11 lines of -17.77. I feel like I am missing just a couple of minor things here but I just can't crack em.

There are a number of errors in the script. The main problem is "while read ct" where the script calculates $ct not $ft and again "while read ft" where the script calculates $ft not $ct. !

Try this (I have removed various spurious lines like "set `cat $input`" and two spurious "echo enter" lines.

Also corrected the spelling of Centigrade and corrected the calculation from Centigrade to Fahrenheit.

Also changed the erroneous "if" test to a "case" statement.

#!/bin/bash
print_dash_line()
{
echo "--------------------     --------------------"
}

input=p3data
output=p3output

echo "1. Convert Centigrade temperature into Fahrenheit temperature."
echo "2. Convert Fahrenheit temperature into Centigrade temperature."
echo -n "Select your choice (1-2): "
read choice
case "$choice" in
1)
  echo " Centigrade Temperature      Fahrenheit Temperature " > ${output}
  print_dash_line >> ${output}
  while read ct
  do
    ft=$(echo "scale=2; ((($ct * 9) / 5) + 32)" |bc)
    echo "             $ct                        $ft " >> $output
    print_dash_line >> $output
  done < ${input}
;;
2)
  echo " Fahrenheit Temperature      Centigrade Temperature " > ${output}
  print_dash_line >> ${output}
  while read ft
  do
    ct=$(echo "scale=2; ((($ft -32) * 5) / 9)" |bc)
    echo "             $ft                        $ct " >> $output
    print_dash_line >> $output
  done < ${input}
;;
*)
    echo "Please select 1 or 2"
    exit l
esac
1 Like

Oh cool the F2C works seemingly very well. There must be a small problem with the C2F formula though because I get a syntax error that reads: (standard_in) 1: syntax error. I have tried rearranging that formula and have tried a different variation of it but I get the same error.

#!/bin/bash
print_dash_line()
{
echo "-----------------------     -----------------------"
}

input=p3data
output=p3output

echo "1. Convert Centigrade temperature into Fahrenheit temperature."
echo "2. Convert Fahrenheit temperature into Centigrade temperature."
echo -n "Select your choice (1-2): "
read choice
case "$choice" in
1)
  echo " Centigrade Temperature      Fahrenheit Temperature " > ${output}
  print_dash_line >> ${output}
  while read ct
  do
    ft=$(echo "scale=2; ((($ct * 9) / 5) +32)" |bc)
    echo "             $ct                        $ft " >> $output
    print_dash_line >> $output
  done < ${input}
;;
2)
  echo "Fahrenheit Temperature     Centigrade Temperature " > ${output}
  print_dash_line >> ${output}
  while read ft
  do
    ct=$(echo "scale=2; ((($ft -32) * 5) / 9)" |bc)
    echo "             $ft               $ct " >> $output
    print_dash_line >> $output
  done < ${input}
;;
*)
    echo "Please select 1 or 2"
    exit l
esac

I cannot reproduce your error. The script you posted is virtually identical to the script I posted bar subtle spacing within the "bc" command.

All I can guess is that the file p3data is in some way corrupt or that the data or script have been edited with a Microsoft editor and contain funny characters.

If you get an error message, please post the error message verbatim and also try running the script with "set -x" on the second line (execute trace) to try to find which line is producing the errror.

1 Like

Well I can't thank you enough methyl and all who pitched in to help with this. It really helped me to grasp some more knowledge of UNIX and I appreciate your time in helping me to achieve this knowledge. Upon looking at the output file the F2C and C2F both work, I do get a syntax error with C2F but it doesn't seem to affect the output file. Well, on to the next one.

A blank line in the file will do this. The error is from bc. If $ct has no value, ( * 9) is not ok, but ( -32) is. You can check for blanks [[ $ct ]] || continue

2 Likes

Many thanks to neutronscott. I can now reproduce the error by inserting a blank line into p3data !
QED.

Yes, thanks a lot neutron, I removed that space and there is no longer an error. :smiley: