从 URL 执行 bash 脚本 [英] Execute bash script from URL
问题描述
假设我在 URLhttp://mywebsite.com/myscript.txt"有一个包含脚本的文件:
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)
但这似乎不像我保存到文件然后执行那样工作.例如 readline 不起作用,输出只是:
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!
同样,我试过:
curl -s http://mywebsite.com/myscript.txt | bash -s --
结果相同.
最初我有一个解决方案:
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.
我知道有关从 URL 运行 shell 脚本的安全问题,但现在让我们忽略所有这些问题.
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)
应该这样做.或者,不要对您的初始重定向进行重定向,即重定向标准输入;bash
需要一个文件名,无需重定向即可正常执行,<(command)
语法提供了一个路径.
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)
如果你看一下echo <(cat/dev/null)
这篇关于从 URL 执行 bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!