【问题标题】:Executing remote script by curl doesn't work通过 curl 执行远程脚本不起作用
【发布时间】:2020-09-21 10:45:05
【问题描述】:

我正在尝试从另一个脚本 (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

你知道如何让它工作吗? 我希望我的脚本在线,这样我就不必一直克隆/下载。

【问题讨论】:

  • 不应该像这样运行从远程服务器通过管道传输的脚本的原因:security.stackexchange.com/q/213401/215097
  • @LéaGris 我明白,但这是我的存储库,所以我认为它很安全,因为没有人可以修改代码。

标签: bash shell ubuntu scripting


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    这可能会达到你想要的:

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

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 2011-05-09
      • 2017-08-02
      • 2015-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多