Command output into a variable

Hi, with this command:

cu -l /dev/ttyACM0 -s 9600 > name.txt

I put the output of the port in a txt

Is posible to do the same (or similar) in a var directly, inside a C program?

cu -l /dev/ttyACM0 -s 9600 > variable ?

I have trying this withs pipes, but i dont know how to adress te port to a pipe.
Also i have tried with function read, fgets, etc.. but I don't why, it doesn't works, just write.

Thank you

Using a POSIX compatible shell (ksh, bash):

variable=$(cu -l /dev/ttyACM0 -s 9600)

You'd probably want to use "popen" for that.

Indeed, I missed the question was about doing it from C.

Hi, thank you very much.
popen seems to works ok. But there s a problem with pclose();

when I write:

file = popen ("cu -l /dev/ttyACM0 -s 9600", "w"); //WRITE
fputs("AT\n",file);
pclose(file)

this run ok. In console:

Connected.
cu: End of file on terminal
Disconnected.

But with:

file = popen ("cu -l /dev/ttyACM0 -s 9600", "r"); //READ

there is no response when I try to close the pipe. Not Disconnected, and so i can't write one more time.

I'm trying to do this, because I need a bidirectional pipe , and the only way I know to do this is to: open to write -- > close --> open to read ---> close and another time the same
BUt pclose function failed in read mode .
thank you

popen is unidirectional, closing and reopening will not allow you to establish any decent communication.

Ever heard of named pipes?

Create a named pipe /tmp/cu.in, use it:
file = popen ("cu -l /dev/ttyACM0 -s 9600 < /tmp/cu.in", "r"); //READ

Then you could write to /tmp/cu.in what you want as input for cu, and retrieve the results with fgets.

But you really should be using some kind of inter-process communication, messaging.

For bidirectional pipes, you can use the "pipe" system call.