Reputation: 1695
I am trying to write a python script which will execute a bash command line program for me. This program asks for user input twice, and I want my script to automatically enter "1" each time.
I've heard of something like this:
os.system("program < prepared_input")
How do I write prepared_input? Thanks.
Upvotes: 3
Views: 10331
Reputation: 3838
Use of pexpect will work for you...
Here is the solution - http://pypi.python.org/pypi/pexpect/
Upvotes: 1
Reputation: 16330
I suppose to make your example work you would need:
prepared_input = "<input goes here>"
os.system("program < {0}".format(prepared_input))
but depending on what you want to do, there are almost certainly better ways to achieve it. If you give us more information about what you're doing and why, we can perhaps suggest some alternatives.
Upvotes: 1
Reputation: 251136
Create file with two lines:
1
1
And use read
in the bash script to get the input:
Demo:
$ cat abc
1
1
$ cat so.sh
#!/bin/bash
read data
echo "You entered $data"
read data
echo "Now you entered $data"
$ bash so.sh <abc
You entered 1
Now you entered 1
Python :
>>> import os
>>> os.system("bash so.sh < abc")
You entered 1
Now you entered 1
0
Upvotes: 5