假设我在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)
小智 80
这是执行远程脚本的方法,并向其传递一些参数(arg1 arg2):
curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2
Run Code Online (Sandbox Code Playgroud)
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读取.
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)
最好的方法是
curl http://domain/path/to/script.sh | bash -s arg1 arg2
Run Code Online (Sandbox Code Playgroud)
这是@ user77115对答案的轻微更改
还:
curl -sL https://.... | sudo bash -
Run Code Online (Sandbox Code Playgroud)
小智 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)