0

I'm trying to write a bash script to run on an Ubuntu system.

The idea is that the script needs to initialize ad lunch a C program. The C code creates a folder and stores data in it coming form an ADC, while it outputs the number of acquisitions.

What a would like to do is to return the name of the folder created in the code to the bash script to be able to copy it. The problem is that, for what I've understood, the code can return only an 8 bit INT, not a whole string. Also using fprint is not a solution because I need to print at screen the number of acquisitions, not only the name of the folder.

Any alternative solutions that I do not know about?

  • 2
    Parse the number out of the output. Or, if the other output isn't meant to be further processable, make the other output go to `stderr` instead of `stdout`. – Petr Skocik Nov 27 '18 at 10:22
  • 1
    add some meta info to message: `adc_output:value`, `directory:value` unless you are expected to print adc info in certain format and can't alter it. – Oo.oO Nov 27 '18 at 10:24

2 Answers2

0

(Bad) Ways of "returning" a string from a C program

As you said, a program can only return one return code. A return code, as its name implies is meant to communicate about the successful execution of the program, not to return data.

In your case, you could return the folder name using a few methods:

Both of these options are bad, because you need to know where the result will be written too (the name of the environment variable of the path of the file)

What you should do

The way to go is to flip the problem around and pass the folder you want your C program to write to as an argument.

op414
  • 582
  • 5
  • 15
  • Perhaps the name of the folder depends on the data received by the program. – vgru Nov 27 '18 at 10:25
  • @Groo indeed, but generating filenames from external input (user/sensors/...) is bad practice anyway (see [this windows specific answer](https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names?answertab=active#tab-top) for examples) – op414 Nov 27 '18 at 10:29
  • But the exact data that OP is trying to return is less relevant to the problem IMHO. It could be any set of values `(number_of_acquisitions, list_of_sensor_names, peak_value)`, which are determined by the program. – vgru Nov 27 '18 at 10:38
0

The program could write the directory name to lets say file descriptor 3 and then capture it via the shell by doing

program 3>dir.txt 
alk
  • 69,737
  • 10
  • 105
  • 255