Python script not waiting for user input when ran from piped bash script

拥有回忆 提交于 2019-12-24 04:31:52

问题


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

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

The bash script then rapidly delegates to a python script:

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

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]: ')
[...]

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.

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) !

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.

Thanks very much for your help !

Things I have also tried:

  • Starting the python script in a subshell: $(/usr/bin/env python3 deploy.py)

回答1:


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

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

or

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


来源:https://stackoverflow.com/questions/25769320/python-script-not-waiting-for-user-input-when-ran-from-piped-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!