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.
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.
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?
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
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.
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
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.
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