Redirecting Output of a C Program to Another C Program with a Bash Script Under Linux

Redirecting output of a C program to another C program with a bash script under Linux

| makes the program to the right read (as STDIN) the STDOUT of the program on the left.

But your program2 does not read STDIN at all. It reads the arguments (which are NOT STDIN).

You should do:

program2 `program1`

Bash evaluates program1 (when it sees the backquotes), and passes it as an arg to program2.

On my keyboard the backtick (`) is to the left of the "1" key, and above my LEFT TAB key.

EDIT:
If the string output of program1 contains spaces and you want the entire string to be interpreted as one argument, quote the string with "" or '':

program2 "`program1`"

Problem redirecting a C program output in bash

Flushing after newlines only works when printing to a terminal, but not necessarily when printing to a file. A quick Google search revealed this page with further information: http://www.pixelbeat.org/programming/stdio_buffering/

See the section titled "Default Buffering modes".

You might have to add some calls to fflush(stdout), after all.

You could also set the buffer size and behavior using setvbuf.

How to call a C program inside bash script and store its return value to a variable?

The return value of a program is store in the $? variable. So you only need to add each $?:

for i in $frames;
do
./enc_ber $bytes $snr $modulation $channel $alamouti
encBER=$((encBER + $?))
done

Note that the value is restricted to eight bits, so the maximum value is 255.


If you want to capture an integer greater than 255 or a float for instance, use stderr for all the things you don't want (fprintf(stderr, "Things I don't want\n");) and stdout to print the return value you want to catch.



Related Topics



Leave a reply



Submit