从 URL 执行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5735666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:44:56  来源:igfitidea点击:

Execute bash script from URL

linuxbashcurl

提问by Tristan

Say I have a file at the URL "http://mywebsite.com/myscript.txt" that contains a script:

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

#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"

And I'd like to run this script without first saving it to a file. How do I do this?

我想在不先将它保存到文件的情况下运行这个脚本。我该怎么做呢?

Now, I've seen the syntax:

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

bash < <(curl -s http://mywebsite.com/myscript.txt)

But this doesn't seem to work like it would if I saved to a file and then executed. For example readline doesn't work, and the output is just:

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

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

Similarly, I've tried:

同样,我试过:

curl -s http://mywebsite.com/myscript.txt | bash -s --

With the same results.

结果相同。

Originally I had a solution like:

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

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

But this seems sloppy, and I'd like a more elegant solution.

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

I'm aware of the security issues regarding running a shell script from a URL, but let's ignore all of that for right now.

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

采纳答案by geekosaur

source <(curl -s http://mywebsite.com/myscript.txt)

ought to do it. Alternately, leave off the initial redirection on yours, which is redirecting standard input; bashtakes a filename to execute just fine without redirection, and <(command)syntax provides a path.

应该这样做。或者,不要对您的初始重定向进行重定向,即重定向标准输入;bash需要一个文件名在没有重定向的情况下执行得很好,并且<(command)语法提供了一个路径。

bash <(curl -s http://mywebsite.com/myscript.txt)

It may be clearer if you look at the output of echo <(cat /dev/null)

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

回答by Random832

Try just:

试试吧:

bash <(curl -s http://mywebsite.com/myscript.txt)

回答by amra

Using wget, which is usually part of default system installation:

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

bash <(wget -qO- http://mywebsite.com/myscript.txt)

回答by luismartingil

You can also do this:

你也可以这样做:

wget -O - https://raw.github.com/luismartingil/commands/master/101_remote2local_wireshark.sh | bash

回答by Anandi Das

This is the way to execute remote script with passing to it some arguments (arg1 arg2):

这是通过传递一些参数(arg1 arg2)来执行远程脚本的方法:

curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2

回答by Thomas Decaux

Also:

还:

curl -sL https://.... | sudo bash -

回答by user77115

For bash, Bourne shell and fish:

对于 bash、Bourne shell 和 fish:

curl -s http://server/path/script.sh | bash -s arg1 arg2

Flag "-s" makes shell read from stdin.

标志“-s”使外壳从标准输入读取。

回答by ling

Just combining amra and user77115's answers:

只需结合 amra 和 user77115 的答案:

wget -qO- https://raw.githubusercontent.com/lingtalfi/TheScientist/master/_bb_autoload/bbstart.sh | bash -s -- -v -v

It executes the bbstart.sh distant script passing it the -v -v options.

它执行 bbstart.sh 远程脚本并传递 -v -v 选项。

回答by Katie MC

Use:

用:

curl -s -L URL_TO_SCRIPT_HERE | bash

For example:

例如:

curl -s -L http://bitly/10hA8iC | bash

回答by Chinglin Wen

I often using the following is enough

我经常使用以下就足够了

curl -s http://mywebsite.com/myscript.txt | sh

But in a old system( kernel2.4 ), it encounter problems, and do the following can solve it, I tried many others, only the following works

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

curl -s http://mywebsite.com/myscript.txt -o a.sh && sh a.sh && rm -f a.sh

Examples

例子

$ 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'
$

The problem may cause by network slow, or bash version too old that can't handle network slow gracefully

问题可能是网速慢,或者bash版本太旧无法优雅处理网速

However, the following solves the problem

但是,以下解决了问题

$ 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
$