I have a console program with an interactive shell, similar to say, the Python interactive shell. Is there an easy way to start this interactive program A and then use another program B to run A? I want to do something like this:
$ /usr/bin/A&
$ #find PID of A somehow
$ somecommand "PID of A" "input string to send to A"
output string from A
$
What kind of "somecommand" could do this? Is this what "expect" is supposed to facilitate? I read the expect
man page but still have no idea what it does.
Best Answer
expect
is for a different purpose. It runs commands on a captive program. You, by contrast, are asking for a way to send commands to a process already running in the background.As a bare-bones minimal example of what you want, let's create a FIFO:
A FIFO is a special file that one process can write to while a different process reads from it. Let's create a process to read from our FIFO file
in
:Now, let's send
python
a command to run from the current shell:The output from
python
is3
and appears here on stdout. If we had redirected python's stdout, it could be sent elsewhere.What
expect
doesexpect
allows you to automate interaction with a captive command. As an example of whatexpect
can do, create a file:Then, run this file with
expect
: