I have a python script I would like to run from a bash script in this way:
#!/bin/bash
python -c "$(< input_file)" &> output_file
In the python script I have some different methods, so the input file contains things like:
from script import *; method_1(); method_2();
The problem is, in both of the methods, they have an input()
method that requires user input (this can't be changed).
So how can I pass an argument in the input_file
(some kind of newline argument) so that it is passed on to the input()
method within method_1()
or method_2()
?
A convenient way to do this is with a "here document":
$ cat myscript
#!/bin/bash
python -c "$(< input_file)" &> output_file << END
3
4
END
Here it is in a self contained test case:
$ cat input_file
height = input("Height:\n")
width = input("Width:\n")
print "Area: ", height*width
$ bash myscript
(no output)
$ cat output_file
Height:
Width:
Area: 12
I believe the input
function just reads from standard input.
So you should just be able to pipe (or redirect) data to that python invocation for input
to pick up I would think.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.