从URL执行bash脚本 [英] Execute bash script from URL

查看:885
本文介绍了从URL执行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有在包含脚本的URLhttp://mywebsite.com/myscript.txt的文件:

 #!/斌/庆典
回声你好,世界!
阅读-p你叫什么名字?名称
回声你好,$ {name}的!

和我想不先保存到一个文件运行此脚本。我该怎么做呢?

现在,我见过的语法:

 庆典< ≤(卷曲-s http://mywebsite.com/myscript.txt)

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

  $庆典< ≤(卷曲-s http://mywebsite.com/myscript.txt)
你好,世界!

同样,我已经试过:

 卷曲-s http://mywebsite.com/myscript.txt |庆典-s  - 

使用相同的结果。

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

 时间戳=`日期+%Y%M%D%H%M%S`
卷曲-s http://mywebsite.com/myscript.txt -o /tmp/.myscript.${timestamp}.tmp
庆典/tmp/.myscript.${timestamp}.tmp
RM -f /tmp/.myscript.${timestamp}.tmp

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

我知道关于从URL运行shell脚本的安全问题,但让我们忽略了这一切的现在。


解决方案

 来源≤(卷曲-s http://mywebsite.com/myscript.txt)

应该这样做。另外,离开了你们都要初始重定向,这是重定向标准输入; 庆典将文件名不进行重新定向到执行就好了,和≤(命令)语法提供了一个路径

 庆典≤(卷曲-s http://mywebsite.com/myscript.txt)

(猫的/ dev / null的)

如果你看看回声&LT的输出可能更清晰

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

#!/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:

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

解决方案

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; bash takes a filename to execute just fine without redirection, and <(command) syntax provides a path.

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

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

这篇关于从URL执行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆