【发布时间】:2014-09-10 15:25:20
【问题描述】:
我正在使用漂亮的命令行构建交互式安装程序:
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 脚本是从一个 bash 脚本运行的,它本身是来自 curl 的 piped,所以当提示出现时,它会自动“跳过”并且一切都像这样结束:
$ 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 的标准输入从 curl 中分离出来,但我没有找到如何去做。
非常感谢您的帮助!
我也尝试过的事情:
- 在子shell中启动python脚本:
$(/usr/bin/env python3 deploy.py)
【问题讨论】: