0

I have a program where I have to take a c program that converts fro kelvin to far and vice versa. I need help with the output, it should read from a data file that I put in $file and the $choice from user and run it the the c program file .farh_kel

while read val
do
      ans=`.fahr_kel $choice $val`
      echo $line
      echo $ans
done < file

MY OUTPUT SHOULD LOOK LIKE THIS

---------------------- -----------------------
           0                   256
---------------------- -----------------------
           32                  273
---------------------- -----------------------
           100                 310
---------------------- -----------------------
           212                 373
---------------------- -----------------------
           108                 315
---------------------- -----------------------
           1243                945
---------------------- -----------------------
           3000                1921
---------------------- -----------------------
            853                02
---------------------- -----------------------
            22                 268
---------------------- -----------------------
           2388                1581
---------------------- -----------------------
            235                 385
---------------------- -----------------------

Instead it looks like this :

---------------------- -----------------------
 0 256
---------------------- -----------------------
 32 273
---------------------- -----------------------
 100 310
---------------------- -----------------------
 212 373
---------------------- -----------------------
 108 315
---------------------- -----------------------
 1243 945
---------------------- -----------------------
 3000 1921
---------------------- -----------------------
 85 302
---------------------- -----------------------
 22 268
---------------------- -----------------------
 2388 1581
---------------------- -----------------------
 235 385
---------------------- -----------------------
5
  • Maybe you just want some quotes around echo "$line" Commented Apr 27, 2017 at 18:38
  • This doesn't really have anything to do with C, and certainly not with cross compiling. Nor is it even specific to Unix or Linux. Tags edited. Commented Apr 27, 2017 at 18:40
  • Putting a . at the beginning of a program name is very strange style. Usually an initial . is used for files and directories that should be hidden by default, like startup and configuration files. Did you mean to write ./fahr_kel? Commented Apr 27, 2017 at 18:41
  • john well eveyone else got it so whats your point? it actually has everything to do with c do to the fact that my coding that run the math for this program is a c code complied in bash Commented Apr 27, 2017 at 19:49
  • sorry barmar I meant fahr_kel Commented Apr 27, 2017 at 19:50

1 Answer 1

2

Quote your variable to prevent the shell from combining all the spaces:

echo "$ans"

Or don't assign it to a variable in the first place, just let the program print its output normally.

while read val
do
    echo "$line"
    .fahr_kel "$choice" "$val"
done < file

In general, you should always quote variables unless you specifically want to allow word splitting and wildcard expansion of its contents.

Sign up to request clarification or add additional context in comments.

2 Comments

having trouble with output project3.sh > project3.output isnt working when placed at the end of my script
@AprilRenee I'm not sure what that means. It sounds like a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.