0
votes

I have a program running on linux

This program take its input from the stdin

So I can launch it with input file in this way

myprogram < file

in order to avoid typing input to the program

Now I want that the program take the input from a command output. something like that

myprogram < anycommand

but this does not work because it's expecting a file and not a command.

How I can make it work? Are there a shell syntax to make it work?

Note: I can not use pipe like anycommand | myprogram

3
Best to explain why can't you use anycmd| myprog? That is a core pattern of all Unix-like programs. Good luck. - shellter

3 Answers

3
votes

normally (IMHO) myprogram does not know anything about file. The bash starts myprogram and reads the file, and writes the content of file to the stdin of myprogram. So myprogram should not know that his stdin is a file. So, anycommand | myprogram must work.

If it doesn't work with ash, maybe you can make a named pipe (mkfifo /tmp/testpipe) Now you can start your program with myprogram < /tmp/testpipe and you can write your input to /tmp/testpipe

1
votes

On my Linux system, ash is a symbolic link to dash and that handles pipes just fine:

pax> ls -ld $(which ash)
lrwxrwxrwx 1 root root 4 Mar  1  2012 /bin/ash -> dash

pax> ash

$ echo hello | tr '[a-z]' '[A-Z]'
HELLO

So I'd give the anycommand | myprogram another shot just in case.

If your ash has no piping capability, you can always revert to using temporary files, provided anycommand isn't a long-lived process that you need to handle the output of in an incremental fashion:

anycommand >/tmp/tempfile
myprogram </tmp/tempfile
0
votes

You need to use it like this:

myprogram < <(anycommand)

This is called process substitution