linux - 从URL执行bash脚本
假设我在包含脚本的URL“[http://mywebsite.com/myscript.txt”]上有一个文件:
#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"
我想在没有先将其保存到文件的情况下运行此脚本。 我该怎么做呢?
现在,我已经看到了语法:
bash <
但是这似乎不像我保存到文件然后执行那样。 例如,readline不起作用,输出只是:
$ bash <
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脚本的安全性问题,但我们现在就忽略所有这些问题。
14个解决方案
164 votes
source
应该这样做。 或者,不要在你的初始重定向,这是重定向标准输入; echo
bash
如果你看一下echo
geekosaur answered 2019-05-31T18:28:04Z
72 votes
这是执行远程脚本的方法,并向其传递一些参数(arg1 arg2):
curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2
Anandi Das answered 2019-05-31T18:28:28Z
26 votes
对于bash:
curl -s http://server/path/script.sh | bash -s arg1 arg2
Bourne shell还支持“-s”从stdin读取。
user77115 answered 2019-05-31T18:28:53Z
14 votes
使用wget,这通常是默认系统安装的一部分:
bash
amra answered 2019-05-31T18:29:17Z
11 votes
试试吧:
bash
Random832 answered 2019-05-31T18:29:41Z
11 votes
你也可以这样做:
wget -O - https://raw.github.com/luismartingil/commands/master/101_remote2local_wireshark.sh | bash
luismartingil answered 2019-05-31T18:30:00Z
9 votes
使用:
curl -s -L URL_TO_SCRIPT_HERE | bash
例如:
curl -s -L http://bitly/10hA8iC | bash
Katie MC answered 2019-05-31T18:30:21Z
8 votes
最好的方法是
curl http://domain/path/to/script.sh | bash -s arg1 arg2
这是@ user77115对答案的轻微更改
Vamsidhar Muggulla answered 2019-05-31T18:30:51Z
4 votes
我经常使用以下就够了
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
$
Chinglin Wen answered 2019-05-31T18:31:37Z
3 votes
也:
curl -sL https://.... | sudo bash -
Thomas Decaux answered 2019-05-31T18:31:56Z
3 votes
只需结合amra和user77115的答案:
wget -qO- https://raw.githubusercontent.com/lingtalfi/TheScientist/master/_bb_autoload/bbstart.sh | bash -s -- -v -v
它执行bbstart.sh远程脚本,传递-v -v选项。
ling answered 2019-05-31T18:32:27Z
1 votes
是一些无人参与的脚本我使用以下命令:
sh -c "$(curl -fsSL )"
我建议避免直接从URL执行脚本。 您应该确保URL是安全的并在执行之前检查脚本的内容,您可以在执行之前使用SHA256校验和来验证文件。
Mauricio Sánchez answered 2019-05-31T18:33:05Z
0 votes
这种方式很好而且很传统:
17:04:59@itqx|~
qx>source
Remote script test...
Param size: 4
---------
17:19:31@node7|/var/www/html/cent74
arch>cat just4Test
echo Remote script test...
echo Param size: $#
javafoot answered 2019-05-31T18:33:29Z
0 votes
bash | curl http://your.url.here/script.txt
实际例子:
juan@juan-MS-7808:~$ bash | curl https://raw.githubusercontent.com/JPHACKER2k18/markwe/master/testapp.sh
Oh, wow im alive
juan@juan-MS-7808:~$
Random memes 2k18 answered 2019-05-31T18:33:53Z