¿Cómo hago para convertir temperaturas usando el programa C en un script de shell?

¿Cómo hago para convertir temperaturas usando el programa C en un script de shell?

Este es el programa formateado en C que se llama 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
 conv_type=atoi(argv[1]);
 temp=atoi(argv[2]);

 // 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);
 else  
 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;
 }

Necesito manipular este programa desde la entrada del usuario en mi script bash.


Este es el archivo de datos que necesito para pasar por el programa C llamado project3.data:

 0
 32
 100
 212
 108
 1243
 3000
 85
 22
 2388
 235

Este es el script que comencé llamado project3.bash.

 #!/bin/bash
 echo -n "Convert from  kelvin to fahrenheit(1) or fahreinheit to kelvin(2)"

 read choice 

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

Solo obtengo la primera fila para el resultado del script. 0 y 256

La salida debe verse así:

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

Respuesta1

Su programa C++ convierte las temperaturas según lo desee y espera un valor de temperatura como segundo argumento de entrada. Sin embargo, su script bash no pasa la temperatura como argumento de entrada. En cambio, llama al programa C++ con elnombre de archivo de datos como argumento de entrada. Esto no es lo que se desea. Debe llamar al programa C++ utilizando los valores de entrada como argumento; es decir, debe modificar el script bash de la siguiente manera.

La solución necesaria en el script bash es llamar a la función c una vez por cada línea de datos y agregar el formato necesario. Esto lo muestro a continuación.

#!/bin/bash

inputFile="project3.data"

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
do
      $cmd $inputVal
      echo $linePrint
done < "$inputFile"
echo $linePrint

Otra forma es cambiar el programa C++ para que espere una entrada de archivo en lugar de una entrada de temperatura.

información relacionada