Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit and from Fahrenheit to Centigrade.

B) Requirements of the script.

1) Your data file is shown in item C.

2) The initial and converted numbers should line up in the center of the proper headings.

3) Numbers should be rounded to the nearest whole number.

4) You must use a looping structure.

5) Fully document all functions and operations within the script and in your program source code.

6) You need to have at least one script call another script and pass command line parameters. �). That second script should perform the calculations from Fahrenheit to Centigrade or from Centigrade to Fahrenheit. (Note�It�s ok to have two, one for each conversion)

C) The contents of the input data file shown below:
1
8
22
43
89
283
120
212
1043
100
287

4) Sample output of the data. Other outputs as your name and date would be additional.

Centigrade Temperature Fahrenheit Temperature


   number centered here                         number centered here

#(can't get them centered, each number should be centered under where it belongs and to the other side it should shows what the number is in centigrade (1F is 17C)

Fahrenheit Temperature               Centigrade Temperature
--------------------------------              ---------------------------------
                 1 -                                                    17
                 8 -                                                    13
                 22 -                                                  5
                 43                                                    6
                89                                                    31
                283                                                  139
               120                                                  48
               212                                                  100
               1043                                                561
               100                                                  37
                287                                                  141

  1. Relevant commands, code, scripts, algorithms:
f=`echo -e "scale=0\n($C*9/5=32)"|bc -1`
echo -e
for in loop
Centigrade to Fahrenheit C = (F - 32) * 5/9
Fahrenheit to Centigrade F=C*9/5+32
  1. The attempts at a solution (include all code and scripts):

the input file is in "inputfile"
scale=0\n to make sure it's rounded.

to convert Centigrade to Fahrenheit

echo -e " Centigrade Temperature  \tFahrenheit Temperature " >inputfile.out
for C in `cat inputfile`
do
f=`echo -e "scale=0\n(C*9/5+32)"|bc -l`
echo -e " $C   \t$f " >>inputfile.out
done
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Old Dominion University Norfolk, VA USA Steven Zeil, CS 252

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

What shell are you using? What operating system are you using?

Does your shell provide a way to get the number of characters in the value of a variable?

If you have the number of characters in the value of a variable and the number characters in the field in which that value is to be centered, can you determine how many spaces and/or tabs need to be displayed before that value to center it in the first column?

If you have the number of characters in the value of the variable to be displayed in the 1st column and the number of characters in the value to be displayed in the 2nd column and the width of the 2nd column heading and the number of spaces between the 1st and 2nd column headings, can you figure out how many spaces and/or tabs need to be displayed between the two values to make the 2nd value appear in the center of the 2nd column?

Is the printf utility one of the utilities you're allowed to use for this assignment? If so, do you know how to specify the width of a string to be printed as a decimal value in the printf format string operand?

PS Please use CODE tags when showing us input and output as well as for shell code that you provide.

I'm using the bash shell. vi
No we haven't learned about printf.
I want to know what is wrong with my code for the convert and the table .
when I run it I get in "inputfile.out" (the output file) :

287           548

287 is the last number from the list in the input file. and 548 is 287C in Fahrenheit and it is rounded.
Why is it not showing every number when it loops through. ( it just showed the last number) and what is wrong with the table headings. I know that it is something worng with the tabling code, or/and the loop syntax.

I'll worry about the numbers being centered in the output table later. I first want the conversion to work and get them displayed in a table like it is shown.

If you don't know about printf yet, don't worry about it. It would make centering text easier, but it can certainly be done with echo.

What happens to the contents of a file when you use the > redirection operator? Have you learned to use any other output redirection operators?

1 Like

Alright. The

> 

deletes the content in the file

>>

appends it.
I changed > to >>
now i got this :

screenshot in attachments.
Something wrong in the tabling script?
What am i missing.

So it looks like you're getting all of your output (although the sample in your assignment showed two lines of headings and your output only shows one). It looks like your code to convert Centigrade to Fahrenheit is working correctly. So, what problem do you have left besides centering your output values under the appropriate headings? (You do realize that since the output values you've calculated vary from one to four characters, you will need to vary the number of spaces or tabs printed in front of the calculated values to center them, don't you?) I assume that you don't have access to the tabs utility yet, so you're probably better off just using spaces in your output rather than tabs. (And even if you do have access to the tabs utility, I wouldn't recommend using it for something like this.)

Do you know how to get bash to tell you how many characters are stored in a variable? Do you know what output column is at the center of each of your headings? With these two pieces of information, can you calculate how many leading spaces or tabs you need to print before the Centigrade value? (I'm not asking if you can do these calculations using bash; I'm asking if you understand the arithmetic required to determine how to center the strings you'll be printing.)

Do you know how to do integer arithmetic operations inside bash (without invoking bc or expr) yet? If not, it isn't a big deal, you have shown that you can use bc to perform any calculations you need to perform; it is just easier to use the bash built-in arithmetic evaluation tools. Do you know how to use a for loop in bash? Do you know how to use echo to output some characters without a trailing newline?

PS When posting code and sample input and output, please don't do anything special with the fonts. Using arial (a variable width font) instead of the default constant width font will make it almost impossible to get things lined up here.