Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass stdout as file name for command line util?

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?

like image 452
Jake Avatar asked Oct 13 '11 15:10

Jake


People also ask

How do I specify stdout as a file?

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.

What is the filename for stdout?

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.

What is stdout in command line?

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.

What is stdout in shell script?

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.


1 Answers

Solution 1: Using process substitution

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

Solution 2: Using named pipes explicitly

You can do the above explicitly / manually as follows:

  1. Create a named pipe using the mkfifo command.

    mkfifo my_buf 
  2. Launch your other command with that file as input

    other_command < my_buf 
  3. Execute foo and let it write it's output to my_buf

    foo -o my_buf 

Solution 3: Using /dev/stdout

You can also use the device file /dev/stdout as follows

foo -o /dev/stdout | other_command 
like image 65
aioobe Avatar answered Sep 21 '22 10:09

aioobe