通过curl执行远程脚本不起作用

时间:2020-06-02 20:31:29

标签: bash shell ubuntu scripting

我正在尝试从另一个脚本( test.sh )运行一个脚本(用于安装和设置Postgres DB)。问题是我的脚本内部调用的脚本是远程的(在GitHub上在线),并且不起作用。

我试图用三种不同的方式来调用脚本。

  1. 本地-工作
  2. 远程使用bash <(curl...-引发错误
  3. 远程使用curl ... | bash -s-下载脚本并挂起...

test.sh

#!/bin/bash

# this works correctly (it's the same but local)
sudo bash postgres/setup.sh 

# this raises "bash: /dev/fd/63: No such file or directory"
sudo bash <(curl -s https://raw.githubusercontent.com/milano-slesarik/sh-ubuntu-instant/master/postgres/setup.sh)

# this hangs
curl https://raw.githubusercontent.com/milano-slesarik/sh-ubuntu-instant/master/postgres/setup.sh | sudo bash -s

最后一个可能会下载脚本,但随后会挂起...

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1532  100  1532    0     0   8281      0 --:--:-- --:--:-- --:--:--  8281

您知道如何使其工作吗? 我希望我的脚本在线,所以不必一直克隆/下载。

2 个答案:

答案 0 :(得分:2)

运行curl -s $script |bash -s时挂起的原因似乎是脚本本身的不当行为,至少在这样运行的情况下如此。您可以通过在-x命令中添加bash标志来调试它。然后,由于脚本中包含while语句,您将看到以下消息无休止地产生:

++ read -p 'Do you wan'\''t to install postgres? [y/n]: ' yn
++ case $yn in

此外,只要您以这种方式运行脚本,为避免此行为,我将删除那些脚本期望用户输入的行。如果不是这种情况,最简单的方法就是通过curl下载它,然后从您的终端本地运行它。

答案 1 :(得分:0)

这可以实现您想要的:

sudo bash -c "bash <(curl -s https://raw.githubusercontent.com/milano-slesarik/sh-ubuntu-instant/master/postgres/setup.sh)"