This is the formatted C program is called fahr_kel:

 #include <stdio.h>
 #include <stdlib.h>
 #include <iostream>
 #include <string>
 #include <vector>

 int main( int argc, char *argv[] )
    if (argc < 3) 
  std::cerr << "Usage:" << argv[0] << " arg1 arg2 \n"
  << "arg1 is the conversion type (1 or 2) \n "
  << "arg 2 is the temp to be converted" << std::endl;
  return 1;
 // Assign the variables used in this program
 int temp, conv_temp, conv_type;

 // assign the input options to variables

 // convert the temps..
 // if the input number is 1, convert from Kelvin to Fahrenheit
 // if the input number is anything else, convert from Fahrenheit to Kelvin
 if (conv_type == 1)
 conv_temp = (((temp - 273) * 1.8) + 32);
 conv_temp = (((( temp - 32 ) * 5 ) / 9 ) + 273 );

 // print the data 
 printf ("       %3.1i                  %3.1i\n",temp,conv_temp);

 // end of main function 
 return 0;

I need to manipulate this program from the user input in my bash script.

This is the data file I need to pass through the C program called project3.data:


This is the script I started called project3.bash.

 echo -n "Convert from  kelvin to fahrenheit(1) or fahreinheit to kelvin(2)"

 read choice 

 /home/username/project3/fahr_kel [ choice project3.data ]

I am only getting the first row for the output of the script. 0 and 256

The output needs to look like this:

 ---------------------- -----------------------
 Fahrenheit Temperature Kelvin Temperature
 --------------------- -----------------------
           0                  256
 ---------------------  -----------------------
           32                 273
 ---------------------- -----------------------
          100                 310
 ---------------------- -----------------------
          212                 373
 ---------------------- -----------------------
          108                 315
 ---------------------- -----------------------
          1243                945
 ---------------------- -----------------------
          3000                1921
 ---------------------- -----------------------
           85                  302
 ---------------------- -----------------------
           22                  268
 ---------------------- -----------------------
          2388                 1581
 ---------------------- -----------------------
          235                   385
 ---------------------- -----------------------

closed as unclear what you're asking by jasonwryan, steeldriver, Gilles 'SO- stop being evil', Stephen Harris, sam Apr 25 '17 at 4:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you trying to write a C program, a C++ program, or a shell script? it's not at all clear from your question as it stands – steeldriver Apr 25 '17 at 0:11
  • I'm sorry. I now realize it didn't make sense I tried to fix it. – Jaruto Apr 25 '17 at 0:25
  • I need to use the C program in my shell script that will convert temperatures in the data file and then output them as shown. – Jaruto Apr 25 '17 at 0:26
  • (1) That’s a C++ program, not a C program.  std::cerr << didn’t exist in C.  (2) What is the script supposed to do?  I guess you want it to ask the user which way the temperatures should be converted, and then process the numbers from the file.  Please say so explicitly; don’t make us guess.  (3) Think about it: What are the arguments to the C++ program?  (Two numbers.)  What arguments are you passing to the C++ program?  (A number and a file name.) See the problem? – Scott Apr 25 '17 at 0:39
  • @Scott The only issue I have now is that only the first row of what the output should look like shows up, I figured out the rest. Any ideas? – Jaruto Apr 25 '17 at 0:58

Your C++ program converts temperatures as desired, and expects a temperature value as the second input argument. However, your bash script does not pass the temperature as input argument. Instead, it calls the c++ program with the data filename as input argument. This is not desired. You should call the c++ program using the input values themselves as argument; i.e. you need to modify the bash script as follows.

The necessary fix in the bash script is to call the c function once for every data line, and to add necessary formating. I show this below.



echo -n "Convert from  kelvin to fahrenheit(1) or fahreinheit to kelvin(2)"

read choice 
# run the following per input data line
#./fahr_kel [ choice project3.data ]
cmd="./fahr_kel $choice "

# print header
linePrint="------------------  ----------------"
echo $linePrint
echo "Fahrenheit Temperature Kelvin Temperature"
echo $linePrint

while read inputVal
      $cmd $inputVal
      echo $linePrint
done < "$inputFile"
echo $linePrint

Another way is to change the c++ program to expect a file input instead of temperature input.

  • You need to change the header too based on the value of $choice. – user218374 Apr 25 '17 at 6:37
  • @RakeshSharma I did that using if statement output is perfect, thanks. Would you happen to know how to capture the output of both options? I tried to run bash project3.bash 1 >> project3.output. But, it did not show correct output. – Jaruto Apr 25 '17 at 13:43
  • @Jaruto, That should work. What is the output you get? – Vignesh Apr 25 '17 at 20:53
  • @Vignesh No problem with the output it is perfect. I just need help to capture it and put it in another file. – Jaruto Apr 25 '17 at 23:49
  • @Jaruto To capture the o/p just add this line after the # print header line: exec 1>SCRIPT.LOG – user218374 Apr 26 '17 at 9:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.