Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to supply stdin/out instead of a file to a program in unix?

Tags:

bash

unix

I have a command line utility from a third party (it's big and written in Java) that I've been using to help me process some data. This utility expects information in a line delimited file and then outputs processed data to STDOUT.

In my testing phases, I was fine with writing some Perl to create a file full of information to be processed and then sending that file to this third party utility, but as I'm nearing putting this code in production, I'd really prefer to just pipe data to this utility directly instead of first writing that data to a file as this would save me the overhead of having to write unneeded information to disk. Is there any way to do this in unix?

Currently I call the utility as follows:

bin/someapp do-action --option1 some_value --input some_file

I'd like to do something like:

bin/someapp do-action --option1 some_value --input $piped_in_data

Is anything like that possible without my modifying the third party app?

like image 815
Eli Avatar asked Nov 03 '10 22:11

Eli


People also ask

How do I exit STDIN?

The simple, non-technical, answer is that Ctrl + D terminates the STDIN file and that Ctrl + C terminates the active application.

What is the difference between command line arguments and STDIN?

Any program cannot directly interact with Keyboard instead they interact with stdin . So if a program needs to take an input from user it will access stdin . Command line arguments are basically a method to take input from used along with the invocation of command to make the command do some specific task.


2 Answers

You should be able to use /dev/stdin:

bin/someapp do-action --option1 some_value --input /dev/stdin

(Note that on some systems, /dev/stdin is a symlink; if your Java program doesn't cope with that, you might have to use /dev/fd/0 or something similar instead.)

like image 121
Gordon Davisson Avatar answered Oct 19 '22 02:10

Gordon Davisson


You can use "process substitution" in bash to achieve something like what you want.

bin/someapp do-action --option1 some_value --input <(generate_input.sh)

should do the trick. The <(list) part is the process substitution.

like image 20
Cameron Skinner Avatar answered Oct 19 '22 03:10

Cameron Skinner