I'm working with a command line utility that requires passing the name of a file to write output to, e.g.
foo -o output.txt
The only thing it writes to stdout
is a message that indicates that it ran successfully. I'd like to be able to pipe everything that is written to output.txt
to another command line utility. My motivation is that output.txt will end up being a 40 GB file that I don't need to keep, and I'd rather pipe the streams than work on massive files in a stepwise manner.
Is there any way in this scenario to pipe the real output (i.e. output.txt
) to another command? Can I somehow magically pass stdout
as the file argument?
Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator. We have created a file named “sample.
the usual "write to stdout" convention for unixy tools is to use - as a filename (i.e. foo -o - ). In your code, you could simply detect that special filename and use stdout for output instead of an fopen ed file. Don't print a status in that case, or print it to stderr which you can redirect separately.
Standard output, sometimes abbreviated stdout, refers to the standardized streams of data that are produced by command line programs (i.e., all-text mode programs) in Linux and other Unix-like operating systems.
Stdout, also known as standard output, is the default file descriptor where a process can write output. In Unix-like operating systems, such as Linux, macOS X, and BSD, stdout is defined by the POSIX standard. Its default file descriptor number is 1. In the terminal, standard output defaults to the user's screen.
The most convenient way of doing this is by using process substitution. In bash the syntax looks as follows:
foo -o >(other_command)
(Note that this is a bashism. There's similar solutions for other shells, but bottom line is that it's not portable.)
You can do the above explicitly / manually as follows:
Create a named pipe using the mkfifo
command.
mkfifo my_buf
Launch your other command with that file as input
other_command < my_buf
Execute foo
and let it write it's output to my_buf
foo -o my_buf
/dev/stdout
You can also use the device file /dev/stdout
as follows
foo -o /dev/stdout | other_command
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With