Click to See Complete Forum and Search --> : Sending input to an interactive program
cjanscen
11-25-2003, 12:51 AM
How can I send input to an interactive program? The program prompts me for a string to enter, and then I have to hit enter again..and I want to be able to do this from a shell script or a perl program...
I tried a simple thing lik this:
#!/usr/bin/perl
use strict;
print "string\n"
and then did:
./script.pl |./program
but it did not work...what do I have to do?
jim mcnamara
11-25-2003, 07:43 AM
Redirect stdin to a script of answers
Suppose you have a script, foo, that asks two questions.
Write the answers to a tmpfile, so the file looks like this:
myfile.dat
no
Next redirect stdin like this and write errors and results out to two log files (for example)
foo < tmpfile 2>error.log > foo.log
rm -f tmpfile
rid3r
11-25-2003, 08:17 AM
program $(/path/to/script.pl)
bwkaz
11-25-2003, 09:01 PM
Originally posted by rid3r
program $(/path/to/script.pl) Only if "program" also has an option to take the input that you want to give it, on its command line.
$(/path/to/script.pl) runs /path/to/script.pl, takes its output, replaces all consecutive whitespace characters with a single space, and substitutes the result in on the command line of "program".
If "program" can't take what you're trying to give it on its command line, then you need to use a pipe (like you were posting) or redirect stdin (like jim mcnamara posted).
If neither of those two work, then something's seriously wrong. You aren't trying to pass a password to /bin/su, are you? Because su won't take the password from anywhere other than the keyboard, and there are better ways of doing root things without a password -- one of them is sudo.