0

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()?

1
  • Any arguments on the python command line following the argument to the -c are interpreted as arguments to pass to the python code (i.e. whatever code is in your input_file). So you could make the code in your input_file read arguments from the command line and then send it whatever you like before the &> output_file part. Commented Feb 12, 2015 at 18:59

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.