I'm writing a shell script which executes a Python script. The Python script halts for a manual enter to be provided. However, I don't want to have to press enter each time to the script. Instead, I want to pass the manual enter from the executing shell script.

Kindly help, if there is a way to do that?

  • Can you provide a link to the python script so we can see how it reads the input? (It might be possible to pass it an input.) However, I'm more interested in why you're writing a shell script to execute a python script? Why not just edit the python script to do what you want? – HalosGhost Jun 23 '14 at 7:04

If there's only one enter required, just use echo and a pipe:

echo | python script.py

echo prints an empty line (with newline) to standard output, and then we redirect that into the input of the Python script using |. When Python reads from standard input it will get an empty line and continue.

If the script wants more than one enter press, you can use the yes command and a pipe:

yes "" | python script.py

The yes command prints the same line to standard output over and over forever. By default the string is y, but here we make it an empty string so it's just newlines forever.

If the enter is required as a response to a particular prompt, and at other times you need different input, use expect, which is "a Tcl extension that lets you automate interactions with interactive programs".

It is possible to write your shell script so that it reads from Python's output and produces new input for it, but I don't especially recommend it. If you want to do that, see IO Redirection in the Advanced Bash Scripting Guide: you will need to clone file descriptors to do that. You can also use mkfifo and make named pipe files, then read from and write to them normally.

To be honest, I would start thinking about alternative approaches if you're reaching the point of doing either of these approaches: shell scripting just isn't built for this and it'll be much more straightforward to write your script in, say, Python itself.

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.