从管道 bash 脚本运行时,Python 脚本不等待用户输入 [英] Python script not waiting for user input when ran from piped bash script

查看:72
本文介绍了从管道 bash 脚本运行时,Python 脚本不等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用漂亮的命令行构建交互式安装程序:

I am building an interactive installer using a nifty command line:

curl -L http://install.example.com | bash

bash 脚本然后迅速委托给 python 脚本:

The bash script then rapidly delegates to a python script:

# file: install.sh
[...]
echo "-=- Welcome -=-"
[...]
/usr/bin/env python3 deploy_p3k.py

并且python脚本本身提示用户输入:

And the python script itself prompts the user for input:

# file: deploy_py3k.py
[...]
input('====> Confirm or enter installation directory [/srv/vhosts/project]: ')
[...]
input('====> Confirm installation [y/n]: ')
[...]

问题:因为 python 脚本是从 bash 脚本本身运行的,该脚本本身是 piped from curl,当提示出现时,它会自动跳过",一切就这样结束:

PROBLEM: Because the python script is ran from a bash script itself being piped from curl, when the prompt comes up, it is automatically "skipped" and everything ends like so:

$ curl -L http://install.example.com | bash
-=- Welcome ! -=-
We have detected you have python3 installed.
====> Confirm or enter installation directory [/srv/vhosts/project]: ====> Confirm installation [y/n]: Installation aborted.

如您所见,脚本不会等待用户输入,因为管道将输入与 curl 输出联系起来.因此,我们有以下问题:

As you can see, the script doesn't wait for user input, because of the pipe which ties the input to the curl output. Thus, we have the following problem:

curl [STDOUT]=>[STDIN] BASH (which executes python script)
= the [STDIN] of the python script is the [STDOUT] of curl (which contains at a EOF) !

我怎样才能保持这个非常有用和简短的命令行 (curl -L http://install.example.com | bash) 并且仍然能够提示用户输入?我应该以某种方式将 python 的 stdin 从 curl 中分离出来,但我没有找到方法.

How can I keep this very useful and short command line (curl -L http://install.example.com | bash) and still be able to prompt the user for input ? I should somehow detach the stdin of python from curl but I didn't find how to do it.

非常感谢您的帮助!

我也尝试过的事情:

  • 在子shell中启动python脚本:$(/usr/bin/env python3 deploy.py)

推荐答案

你总是可以从控制 tty 重定向标准输入,假设有一个:

You can always redirect standard input from the controlling tty, assuming there is one:

/usr/bin/env python3 deploy_p3k.py < /dev/tty

/usr/bin/env python3 deploy_p3k.py <&1

这篇关于从管道 bash 脚本运行时,Python 脚本不等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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