1

I have a python script which sometimes prompts the user for input in the form of
raw_input('please provide foo').

I have a bash script which among other things executes that python script:

NUM_OUTPUT=$(python -i create_lookup_txtfiles.py data-dir=/data/ad6813/pipe-data/Bluebox/raw_data/dump to-dir=/data/ad6813/caffe/data_info/$TASK_NAME bad-min=$BAD_MIN)

I would like to run this bash script and provide input (as a user, via interactive shell) to the python script's prompts when need be.

But when I run the bash script, I do not see the python prompts.

I have also tried without the -i flag, to no avail.
Is there a way to solve this?

4
  • How are you running the python script in the shell script? Commented Jul 24, 2014 at 3:05
  • do you want user input or a scripted auto input? Commented Jul 24, 2014 at 3:07
  • How does the python script ask for input? Does it detect its running environment? Commented Jul 24, 2014 at 3:13
  • @EtanReisner python script asks for input with raw_input('give me foo'). I don't know how to determine whether it detects its running environment, could you tell me how? Commented Jul 24, 2014 at 3:19

1 Answer 1

0

It is hard to distinguish between the prompt string and the script output, so need to use a different file to write your prompt from Python. An example with tty is below, but you can also use stderr for prompts:

bash script:

m=$(python d.py)
echo "OUTPUT $m"

python script:

import os
tty=os.open("/dev/tty", os.O_RDWR)
os.write(tty, "Enter> ");
s=raw_input("")
print s

the same bash script should also work with stderr prompts:

python script:

import sys 
sys.stderr.write("Enter> ");
s=raw_input("")
print s
Sign up to request clarification or add additional context in comments.

3 Comments

write all interactive messages to stderr from python. only put the thing you want to capture to stdout.
Why use "print s" then? Shouldn't he just print using stderr?
@Kasisnu, that's also true, if all prompts go to stdout and the capture goes to the stderr then from bash these handles can be swapped .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.