从url运行bash脚本时,如何获取用户输入?

从url运行bash脚本时,如何获取用户输入?,bash,shell,curl,command-line,Bash,Shell,Curl,Command Line,考虑以下bash脚本: #!/bin/bash while true; do read -p "Give me an answer ? y/n : " yn case $yn in [Yy]* ) answer=true ; break;; [Nn]* ) answer=false ; break;; * ) echo "Please answer yes or no.";; esac done if $answer

考虑以下bash脚本:

#!/bin/bash
while true; do
    read -p "Give me an answer ? y/n : " yn
    case $yn in
        [Yy]* ) answer=true ; break;;
        [Nn]* ) answer=false ; break;;
        * ) echo "Please answer yes or no.";;
    esac
  done

if $answer 
  then 
    echo "Doing something as you answered yes"
      else 
    echo "Not doing anything as you answered no" 
fi
使用以下命令行从命令行运行时:

$ ./script-name.sh
脚本等待您回答yn,它的工作原理与预期一样

但是,当我上载到url并尝试使用以下方式运行它时:

$ curl http://path.to/script-name.sh | bash
我陷入了一个永久性的循环,脚本说
请回答是或否。
显然,脚本正在接收某种输入,而不是yn


为什么会这样?更重要的是,如何通过从url调用的bash脚本实现用户输入?

或许可以使用显式本地重定向:

read answer < /dev/tty
阅读答案
可能使用显式本地重定向:

read answer < /dev/tty
阅读答案
您可以这样运行它:

bash -c "$(curl -s http://path.to/script-name.sh)"

因为您正在向
bash
解释器提供bash脚本的内容。使用
curl-s
进行静默执行。

您可以这样运行它:

bash -c "$(curl -s http://path.to/script-name.sh)"

因为您正在向
bash
解释器提供bash脚本的内容。使用
curl-s
进行静默执行。

我对这个答案的问题是,读取发生在解释键入内容的回音之前…@DavidSulpy尝试以下操作:
read-p“输入答案:”答案
这个答案的问题是,读取发生在解释要键入什么的回声之前…@DavidSulpy尝试以下操作:
read-p“输入答案:”答案
您已经重定向了shell的标准输入。这就是你的剧本的来源。因此,当bash尝试从标准输入读取更多信息时,它只会得到
EOF
并旋转。Try
read-p“给我一个答案?y/n:“yn
。cat script-name.sh | bash也不会比curl命令工作得更好……您已经为shell重定向了标准输入。这就是你的剧本的来源。因此,当bash试图从标准输入读取更多信息时,它只会得到
EOF
并旋转。Try
read-p“给我一个答案?y/n:“yn
。cat script-name.sh | bash也不会比curl命令工作得更好。。。