views:

480

answers:

2

I have a script that calls an application that requires user input, e.g. run app that requires user to type in 'Y' or 'N'.
How can I get the shell script not to ask the user for the input but rather use the value from a predefined variable in the script?

In my case there will be two questions that require input.

+2  A: 

You can pipe in whatever text you'd like on stdin and it will be just the same as having the user type it themselves. For example to simulating typing "Y" just use:

echo "Y" | myapp

or using a shell variable:

echo $ANSWER | myapp

There is also a unix command called "yes" that outputs a continuous stream of "y" for apps that ask lots of questions that you just want to answer in the affirmative.

Marc Novakowski
The echo is what I need but with multiple parameters...
In that case, try using the "printf" command rather than echo, and include newlines in the string. So if you want to answer "Y" then "N" use: printf "Y\nN\n" | myapp
Marc Novakowski
A: 

the expect command for more complicated situations, you system should have it. Haven't used it much myself, but I suspect its what you're looking for.

$ man expect

http://oreilly.com/catalog/expect/chapter/ch03.html

jskulski
Dont seem to have 'expect' here - running on HPUX11
Install it. expect is an invaluable tool.
PEZ