How to use the output of a command as an input to other program in linux?
Asked Answered
P

3

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

Phira answered 29/4, 2014 at 9:54 Comment(1)
Best to explain why can't you use anycmd| myprog? That is a core pattern of all Unix-like programs. Good luck.Photoflash
B
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

Blocker answered 29/4, 2014 at 10:30 Comment(0)
B
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
Bigamy answered 29/4, 2014 at 10:10 Comment(0)
B
0

You need to use it like this:

myprogram < <(anycommand)

This is called process substitution

Busoni answered 29/4, 2014 at 9:55 Comment(4)
thanks for the answer. I tried your command but it returns the following error: -ash: syntax error: unexpected redirectionPhira
I am not sure if ash supports this, can you use bash?Busoni
I don't have a way to test it in ash since I don't have it. Is there any online tester for this?Busoni
I looked at linux.about.com/library/cmd/blcmdl1_ash.htm and didn't find any process substitution. Did you try: anycommand|myprogram? What error you got?Busoni

© 2022 - 2024 — McMap. All rights reserved.