bash脚本执行一个提示用户的python脚本 [英] bash script which executes a python script which prompts user

查看:48
本文介绍了bash脚本执行一个提示用户的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,有时会提示用户以
的形式输入 raw_input('请提供foo').

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

我有一个bash脚本,除其他外,它执行该python脚本:

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)

我想运行此bash脚本,并在需要时(作为用户,通过交互式shell)向python脚本的提示提供输入.

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.

但是当我运行bash脚本时,看不到python提示符.

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

我也尝试了不使用 -i 标志的情况,但无济于事.
有办法解决吗?

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

推荐答案

很难区分提示字符串和脚本输出,因此需要使用其他文件从Python编写提示.下面是一个带有tty的示例,但是您也可以将stderr用于提示:

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脚本:

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

python脚本:

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

相同的bash脚本也应与stderr提示一起使用:

the same bash script should also work with stderr prompts:

python脚本:

import sys 
sys.stderr.write("Enter> ");
s=raw_input("")
print s

这篇关于bash脚本执行一个提示用户的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆