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?

Alexandre Holden Daly
  • 6,714
  • 5
  • 20
  • 35

1 Answers1

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
perreal
  • 85,397
  • 16
  • 134
  • 168
  • 2
    write all interactive messages to stderr from python. only put the thing you want to capture to stdout. – perreal Jul 24 '14 at 03:34
  • Why use "print s" then? Shouldn't he just print using stderr? – Kasisnu Jul 24 '14 at 03:51
  • 1
    @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 . – perreal Jul 24 '14 at 03:55