PJx
PJx

Reputation: 1103

Python and Bash: how to automate user input?

I downloaded some scripts in both python and bash that prompt the user for command line input. Since I run these scripts often, I would like to automatically supply the input to the programs. I prefer not to modify the scripts. Is there a way to do it without modifying the original code?

[UPDATE] thanks to EnabrenTane's advice, it seems to work pretty well, until I got to a line that read password = getpass.getpass('password: '). It complains the following:

File "/usr/lib/python2.4/getpass.py", line 29, in unix_getpass
old = termios.tcgetattr(fd)     # a copy to save
termios.error: (25, 'Inappropriate ioctl for device')

Any way to get around that?

Upvotes: 0

Views: 3517

Answers (3)

EnabrenTane
EnabrenTane

Reputation: 7466

Like this on bash: $ ./python_script < input.txt

edit: Alternatively you could write your scripts to take ARGV as a file name to read from. You could reopen STDIN to the file and not have to change any other lines.

Upvotes: 3

PJx
PJx

Reputation: 1103

Due to the password requirement, I ended up using pexpect, a python module that automates user input.

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226654

Since the responses in via stdin:

cat answers | yourscript.py

Upvotes: 1

Related Questions