2011-04-20 233 views
109

说我有一个包含脚本的URL“http://mywebsite.com/myscript.txt”文件:执行bash脚本

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

而且我想运行此脚本而不是先将它保存到文件中。我该怎么做呢?现在

,我见过的语法:

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

但这似乎并不像它想如果我保存到一个文件,然后执行工作。例如readline的不工作,并且输出就是:

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

同样,我已经试过:

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

具有相同的结果。

我本来喜欢一个解决方案:

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 

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

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

+1

如果你最终创建一个临时文件,你应该使用'mktemp'而不是滚动你自己的解决方案 – Hasturkun 2011-04-20 19:44:23

+0

'cmd << foo'在大多数shell中是heredoc语法,可能不是你想要的。 – dietbuddha 2011-04-20 20:52:10

回答

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

应该做到这一点。或者,不要在你的初始重定向上重定向标准输入; bash需要文件名才能在没有重定向的情况下执行正常,并且语法提供了一条路径。

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

如果你看看echo <(cat /dev/null)

+0

谢谢,这说明了发生了什么事。 只是好奇,使用该初始重定向的优点是什么?我问,因为RVM的安装,他们使用的命令: '的bash <<(卷曲-s https://rvm.beginrescueend.com/install/rvm)' 为什么不干脆: '的bash <(卷曲-s https://rvm.beginrescueend.com/install/rvm)' – Tristan 2011-04-20 20:49:31

+0

@Tristan:如果他们正在运行交互式安装脚本/程序(商业安装程序包或安装程序,同时还支持自定义安装类型),他们可以提供默认答案。 – geekosaur 2011-04-20 20:52:52

+3

小记:如果wget可用,但curl不可用(例如在一个Ubuntu桌面系统上),你可以用'wget -q http://mywebsite.com/myscript.txt -O -'替换'curl -s http :// mywebsite.com/myscript.txt')。 – 2012-08-21 22:47:18

10

尽量只:

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

输出使用wget,通常是系统默认安装的一部分,它可以更清楚:

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

什么是-qO-? – CodeGuru 2017-11-02 08:34:52

+0

RTFM:https://www.gnu.org/software/wget/manual/wget.html。 ||| -q == --quiet ==“关闭Wget的输出。” ||| -O- == --output-document = - ==如果使用' - '作为文件,文档将被打印到标准输出。 – amra 2017-11-20 11:26:35

9

你也可以这样做:

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

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

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

这打破了stty:\ use'bash <(curl ...)'如果你使用stdin – 2016-01-15 10:04:42

2

另外:

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

最后一个strip的含义是什么意思? – towry 2015-02-23 07:59:04

+0

从bash手册页:A - 表示选项结束并禁用进一步的选项处理。 - 之后的任何参数都被视为文件名和参数。一个论点 - 相当于 - 。 – 2015-06-30 08:59:39

21

对于bash:

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

Bourne shell中还支持 “-s” 从标准输入读取。

3

只是结合AMRA和user77115的答案:

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

它执行bbstart。sh遥远的脚本通过它-v -v选项。

6

curl -s -L URL_TO_SCRIPT_HERE |庆典

例如::

curl -s -L http://bitly/10hA8iC | bash 
4
我经常使用下面的

足够

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

但在旧系统(kernel2.4),它遇到的问题,并执行以下操作可以解决这个问题,我尝试了很多其他的,只有下面的作品

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

例子

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

的问题可能是由网络速度慢,或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 
$ 
6

以最好的方式做到这一点是

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

这是答案的由@ user77115

略有变化