bfops
bfops

Reputation: 5678

Providing partial input to a bash command

Say I'm running some command foo, which prompts the user for various things. I want to provide values for the first few prompts, but enter the rest manually (i.e. on stdin).

How can I do this? I've tried

echo -e "foo\nbar\nbaz" | foo

This accepts all the inputs, but then gets an EOF from the input stream. I've also tried

foo <(echo -e "foo\nbar\nbaz" & cat /dev/stdin)

which didn't work either.

Upvotes: 2

Views: 581

Answers (2)

ruakh
ruakh

Reputation: 183321

The main problem here is most likely that foo is not designed to take a filename as an argument. (Keep in mind that <(...) doesn't pass ...'s output on standard-input; rather, it gets expanded to a special filename that can be read from to obtain ...'s output.) To fix this, you can add another <:

foo < <(echo -e "foo\nbar\nbaz" ; cat /dev/stdin)

or use a pipeline:

{ echo -e "foo\nbar\nbaz" ; cat /dev/stdin ; } | foo

(Note that I changed the & to ;, by the way. The former would work, but is a bit strange, given that you intend for echo to handle the first several inputs.)

Upvotes: 3

Ochi
Ochi

Reputation: 1478

ask user for what you want and then relay that to your command

echo "Question 1: "; read ans1;
echo "Question 2: "; read ans2;
./foo bar bar "$ans1" baz "$ans2"

maybe like that? it's simple and efficient :)

Upvotes: 0

Related Questions