0

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

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 2
    Best to explain why can't you use `anycmd| myprog`? That is a core pattern of all Unix-like programs. Good luck. – shellter Apr 29 '14 at 11:44

3 Answers3

3

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

Bongo
  • 2,933
  • 5
  • 36
  • 67
Biber
  • 709
  • 6
  • 19
2

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

You need to use it like this:

myprogram < <(anycommand)

This is called process substitution

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • thanks for the answer. I tried your command but it returns the following error: `-ash: syntax error: unexpected redirection` – MOHAMED Apr 29 '14 at 09:59
  • I am not sure if `ash` supports this, can you use `bash`? – anubhava Apr 29 '14 at 10:00
  • I don't have a way to test it in `ash` since I don't have it. Is there any online tester for this? – anubhava Apr 29 '14 at 10:02
  • I looked at http://linux.about.com/library/cmd/blcmdl1_ash.htm and didn't find any process substitution. Did you try: `anycommand|myprogram`? What error you got? – anubhava Apr 29 '14 at 10:16