Oneliner 从在线 (Gist) 加载 Lua 脚本并在当前上下文中加载 运行
Oneliner to load Lua script from online (Gist) and run in current context
我有一个 lua REPL,并且想要 运行 一个 lua 脚本文件以纯文本格式存储在 HTTPS://URL
。我了解 os.execute()
可以 运行 OS 命令,因此我们可以使用 curl
等来获取脚本然后 load()
。这在 lua REPL 中可以用一行来完成吗?
Note: If you're going to run source code directly from the web, use https
at least, to avoid easy MitM attacks.
要回答这个问题,因为 Egor 可能不会 post 这样:
(loadstring or load)(io.popen("wget -qO- https://i.imgur.com/91HtaFp.gif"):read"*a")()
为什么打印 Hello world
:
loadstring or load
将与不同的 Lua 版本兼容,因为函数 loadstring
和 load
在某些时候被合并(我相信是 5.2)。 io.popen
在 shell 和 returns 中执行它的第一个参数指向它的标准输出的文件指针。
来自 Egor 的 "gif" 并不是真正的 GIF(在您的浏览器中打开它:view-source:https://i.imgur.com/91HtaFp.gif
)而是一个包含以下文本的纯文本文件:
GIF89a=GIF89a
print'Hello world'
基本上 GIF 以 GIF89a
开头,之后的 =GIF89a
只是为了生成有效的 Lua,这意味着您不必使用 imgur 或 gif,您可以很好地使用原始要点或 github.
现在,当 io.popen
不可用时,os.execute
不太可能在沙箱中可用,但如果可用,您可以使用 os.execute
和临时文件
让我们先把它写出来,因为在一行中它会有点复杂:
(function(u,f)
-- get a temp file name, Windows prefixes those with a \, so remove that
f=f or os.tmpname():gsub('^\','')
-- run curl, make it output into our temp file
os.execute(('curl -s "%s" -o "%s"'):format(u,f))
-- load/run temp file
loadfile(f)()
os.remove(f)
end)("https://i.imgur.com/91HtaFp.gif");
您可以通过删除注释、制表符和换行符轻松地将其压缩成一行:
(function(u,f)f=f or os.tmpname():gsub('^\','')os.execute(('curl -s "%s" -o "%s"'):format(u,f))loadfile(f)()os.remove(f)end)("https://i.imgur.com/91HtaFp.gif");
我有一个 lua REPL,并且想要 运行 一个 lua 脚本文件以纯文本格式存储在 HTTPS://URL
。我了解 os.execute()
可以 运行 OS 命令,因此我们可以使用 curl
等来获取脚本然后 load()
。这在 lua REPL 中可以用一行来完成吗?
Note: If you're going to run source code directly from the web, use
https
at least, to avoid easy MitM attacks.
要回答这个问题,因为 Egor 可能不会 post 这样:
(loadstring or load)(io.popen("wget -qO- https://i.imgur.com/91HtaFp.gif"):read"*a")()
为什么打印 Hello world
:
loadstring or load
将与不同的 Lua 版本兼容,因为函数 loadstring
和 load
在某些时候被合并(我相信是 5.2)。 io.popen
在 shell 和 returns 中执行它的第一个参数指向它的标准输出的文件指针。
来自 Egor 的 "gif" 并不是真正的 GIF(在您的浏览器中打开它:view-source:https://i.imgur.com/91HtaFp.gif
)而是一个包含以下文本的纯文本文件:
GIF89a=GIF89a
print'Hello world'
基本上 GIF 以 GIF89a
开头,之后的 =GIF89a
只是为了生成有效的 Lua,这意味着您不必使用 imgur 或 gif,您可以很好地使用原始要点或 github.
现在,当 io.popen
不可用时,os.execute
不太可能在沙箱中可用,但如果可用,您可以使用 os.execute
和临时文件
让我们先把它写出来,因为在一行中它会有点复杂:
(function(u,f)
-- get a temp file name, Windows prefixes those with a \, so remove that
f=f or os.tmpname():gsub('^\','')
-- run curl, make it output into our temp file
os.execute(('curl -s "%s" -o "%s"'):format(u,f))
-- load/run temp file
loadfile(f)()
os.remove(f)
end)("https://i.imgur.com/91HtaFp.gif");
您可以通过删除注释、制表符和换行符轻松地将其压缩成一行:
(function(u,f)f=f or os.tmpname():gsub('^\','')os.execute(('curl -s "%s" -o "%s"'):format(u,f))loadfile(f)()os.remove(f)end)("https://i.imgur.com/91HtaFp.gif");