从管道bash脚本运行时,Python脚本不等待用户输入

时间:2014-09-10 15:25:21

标签: python linux bash pipe

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

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

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

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

python脚本本身会提示用户输入:

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

问题:因为python脚本是从curl脚本本身运行的pipe d,当提示出现时,会自动“跳过”并且所有内容都如此结束:

$ 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输出。因此,我们有以下问题:

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中分离出来,但我没有找到怎么做。

非常感谢你的帮助!

我尝试过的事情

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

1 个答案:

答案 0 :(得分:2)

您可以随时重定向来自控制tty的标准输入,假设有一个:

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

/usr/bin/env python3 deploy_p3k.py <&1
相关问题