Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a bash script that enters text into programs

Tags:

python

bash

I'm writing a bash script that fires up python and then enters some simple commands before exiting. I've got it firing up python ok, but how do I make the script simulate keyboard input in the python shell, as though a person were doing it?

like image 656
Trindaz Avatar asked Dec 07 '25 12:12

Trindaz


2 Answers

Use a "here" document. It looks like this

command << HERE
text that someone types in
more text
HERE

You don'th have to use "HERE", you can use something that has a little more meaning relative to the context of your code.

like image 57
Chris Avatar answered Dec 10 '25 00:12

Chris


If you really need to simulate typing into the Python interpreter, rather than piping a command to python, you can probably do this with expect

expect should be available in your distribution's repository. For details,

man expect
like image 21
Michael Berkowski Avatar answered Dec 10 '25 00:12

Michael Berkowski