从URL执行bash脚本

Tri*_*tan 163 linux bash curl

假设我在URL"http://mywebsite.com/myscript.txt"上有一个包含脚本的文件:

#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"
Run Code Online (Sandbox Code Playgroud)

我想在没有先将其保存到文件的情况下运行此脚本.我该怎么做呢?

现在,我已经看到了语法:

bash < <(curl -s http://mywebsite.com/myscript.txt)
Run Code Online (Sandbox Code Playgroud)

但是这似乎不像我保存到文件然后执行那样.例如,readline不起作用,输出只是:

$ bash < <(curl -s http://mywebsite.com/myscript.txt)
Hello, world!
Run Code Online (Sandbox Code Playgroud)

同样,我试过:

curl -s http://mywebsite.com/myscript.txt | bash -s --
Run Code Online (Sandbox Code Playgroud)

结果相同.

最初我有一个解决方案,如:

timestamp=`date +%Y%m%d%H%M%S`
curl -s http://mywebsite.com/myscript.txt -o /tmp/.myscript.${timestamp}.tmp
bash /tmp/.myscript.${timestamp}.tmp
rm -f /tmp/.myscript.${timestamp}.tmp
Run Code Online (Sandbox Code Playgroud)

但这似乎很草率,我想要一个更优雅的解决方案.

我知道有关从URL运行shell脚本的安全性问题,但我们现在就忽略所有这些问题.

gee*_*aur 197

source <(curl -s http://mywebsite.com/myscript.txt)
Run Code Online (Sandbox Code Playgroud)

应该这样做.或者,不要在你的初始重定向,这是重定向标准输入; bash采用文件名执行就好没有重定向,<(command)语法提供了一个路径.

bash <(curl -s http://mywebsite.com/myscript.txt)
Run Code Online (Sandbox Code Playgroud)

如果你看一下输出,可能会更清楚 echo <(cat /dev/null)

  • 请注意,您无法将命令行参数传递给脚本.`bash will_not_work foobar <(curl -s http://example.com/myscript.sh)`如果您拥有该脚本,您可以使用环境变量,如下所示:`MYFLAG1 = will_work bash MYFLAG2 = foobar <(curl -s http ://example.com/myscript.sh)`它也适用于这样的管道:`curl -s http://example.com/myscript.sh | MYFLAG1 = will_work MYFLAG2 = foobar bash`这当然要求你使用MYFLAG1和MYFLAG2而不是$ 1和$ 2 (5认同)
  • 小注意:如果wget可用但curl不可用(例如在库存Ubuntu桌面系统上),你可以用`wget -q http://mywebsite.com/myscript.txt -O -`代替`curl -s http: // mywebsite.com/myscript.txt`). (4认同)
  • $ sudo bash &lt;(curl -s http:// xxx)得到错误:bash:/ dev / fd / 63:错误的文件描述符 (2认同)
  • 第一个解决方案(使用源的解决方案)根本不起作用.第二个工作有点但有其局限性.它正在子shell中从URL执行脚本.我在脚本中定义了一些我想在父级中使用的函数.反正有没有实现这一目标?或者我运气不好,唯一的解决方案是将该脚本复制到一个临时文件中然后获取它? (2认同)

小智 80

这是执行远程脚本的方法,并向其传递一些参数(arg1 arg2):

curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2
Run Code Online (Sandbox Code Playgroud)

  • 这打破了stty:\ use`bash <(curl ...)`如果你使用stdin (2认同)

use*_*115 36

对于bash:

curl -s http://server/path/script.sh | bash -s arg1 arg2
Run Code Online (Sandbox Code Playgroud)

Bourne shell还支持"-s"从stdin读取.

  • 例如,这应该适用于大多数 shell,而接受的答案不适用于 *fish*。 (2认同)

amr*_*mra 15

使用wget,这通常是默认系统安装的一部分:

bash <(wget -qO- http://mywebsite.com/myscript.txt)
Run Code Online (Sandbox Code Playgroud)


Ran*_*832 12

试试吧:

bash <(curl -s http://mywebsite.com/myscript.txt)
Run Code Online (Sandbox Code Playgroud)


小智 12

使用:

curl -s -L URL_TO_SCRIPT_HERE | bash
Run Code Online (Sandbox Code Playgroud)

例如:

curl -s -L http://bitly/10hA8iC | bash
Run Code Online (Sandbox Code Playgroud)


lui*_*gil 11

你也可以这样做:

wget -O - https://raw.github.com/luismartingil/commands/master/101_remote2local_wireshark.sh | bash
Run Code Online (Sandbox Code Playgroud)


Vam*_*lla 9

最好的方法是

curl http://domain/path/to/script.sh | bash -s arg1 arg2
Run Code Online (Sandbox Code Playgroud)

这是@ user77115对答案的轻微更改


Tho*_*aux 7

还:

curl -sL https://.... | sudo bash -
Run Code Online (Sandbox Code Playgroud)

  • 最后一条是什么意思? (5认同)
  • 从 bash 手册页: A -- 表示选项结束并禁用进一步的选项处理。-- 之后的任何参数都被视为文件名和参数。- 的参数等效于 --。 (2认同)

小智 5

我经常用下面的就够了

curl -s http://mywebsite.com/myscript.txt | sh
Run Code Online (Sandbox Code Playgroud)

但是在一个旧的系统(kernel2.4)中,它遇到了问题,并且执行以下操作可以解决它,我尝试了很多其他方法,只有以下工作

curl -s http://mywebsite.com/myscript.txt -o a.sh && sh a.sh && rm -f a.sh
Run Code Online (Sandbox Code Playgroud)

例子

$ curl -s someurl | sh
Starting to insert crontab
sh: _name}.sh: command not found
sh: line 208: syntax error near unexpected token `then'
sh: line 208: ` -eq 0 ]]; then'
$
Run Code Online (Sandbox Code Playgroud)

该问题可能是由于网络运行缓慢或bash版本过旧而无法正常处理网络运行缓慢

但是,以下解决了问题

$ curl -s someurl -o a.sh && sh a.sh && rm -f a.sh
Starting to insert crontab
Insert crontab entry is ok.
Insert crontab is done.
okay
$
Run Code Online (Sandbox Code Playgroud)