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.

  • I am not sure if I understand. How you write it, I expect somecommand to be a command belonging to your interactive shell A. So how would a different program B be defined? (sure this is not an XY problem?) – Bernhard Sep 26 '14 at 7:29

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:

$ mkfifo in

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:

$ python <in &
[1] 3264

Now, let's send python a command to run from the current shell:

$ echo "print 1+2" >in
$ 3

The output from python is 3 and appears here on stdout. If we had redirected python's stdout, it could be sent elsewhere.

What expect does

expect allows you to automate interaction with a captive command. As an example of what expect can do, create a file:

#!/usr/bin/expect --
spawn python
expect ">>>"
send "print 1+2\r"
expect ">>>"

Then, run this file with expect:

$ expect myfile
spawn python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 1+2
| improve this answer | |
  • Thank you sir, that was exactly what I was wondering. It's too bad man pages can't be as well written as your comment! – user3268289 Sep 26 '14 at 19:32
  • this was very helpful, however some interactive prompts would need a tail -f in | python &-like syntax – phil294 Aug 8 '17 at 23:51
  • @Blauhirn Interesting. Can you give me an example? – John1024 Aug 8 '17 at 23:58
  • @John1024 yes: try your python example yourself. the print command can be executed only once, the python shell stops after that. but concerning my previous comment: you'd need that for wish: while wish <in & does work, it only accepts the first interaction (echo, cat instance). I needed to write tail -f in |wish & for it to handle multiple echos. -thanks – phil294 Aug 9 '17 at 0:05
  • Yes, I see that. On the other hand, tail -f in | python is not working for me at all: the python process hangs without ever responding. – John1024 Aug 9 '17 at 0:19

Yes, that is the sort of thing you can do with expect, where your expect script is your program B. Basically, it expects some string (maybe a command prompt) and then sends a response.

Check out autoexpect which will help generate your expect script by recording a session. If you play with it a bit and inspect its output, you will get a better idea of how it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.