3

I have a command I want to run, let's call it program. It is an interactive program similar to bash or python REPL.

I want to start this program with some predefined input. I tried program << "some_commands". But after executing some_commands it finishes and I cannot continue interacting manually.

If this program was bash I would use .bashrc for this purpose.

What is the correct way to use such a program with the predefined starting input?

4

You can also do this without using expect:

{ echo foo ; cat ; } | command
3

expect is the tool for this job:

#!/usr/bin/expect -f
spawn program
expect "I'm ready for input"
send "command 1\r"
expect "I'm ready for input"
send "command 2\r"
# ...
send "command last\r"
interact

The interact command returns control to the user.

2

You can use rlwrap to interact with your program. Rlwrap provides a line editor based on the readline library, the same that bash uses. This is typically not as good as the line editor provided by programs that do provide one (e.g. you won't get any syntax coloring or completion), but it's a lot nicer than the default terminal interface which understands only backspace.

rlwrap -P 'some_commands' program

The text is passed into the input buffer but not sent to the program, so you'll have to press Enter after starting the program. The whole text is passed as a single string. It can contain newlines.

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.