我想在某些条件下退出Lua脚本的执行.示例:
content = get_content() if not content then -- ( Here i want some kind of exit function ) next_content = get_content() --example there can lot of further checks
在这里我希望如果我没有得到内容我的脚本假设终止是不应该去检查下一个.
fre*_*doo 17
os.exit()
Run Code Online (Sandbox Code Playgroud)
通过发送信号来杀死进程
do return end
Run Code Online (Sandbox Code Playgroud)
停止执行
如果要在通过使用-i标志启动程序来停止执行后在解释器中编写和执行某些luacode,则这两种方法不相等.
th -i main.lua
Run Code Online (Sandbox Code Playgroud)
从lua api doc中提取:
出于语法原因,中断或返回只能作为块的最后一个语句出现(换句话说,作为块中的最后一个语句或者在结束之前,在else之前或者之前).例如,在下一个示例中,break是then块的最后一个语句.
local i = 1
while a[i] do
if a[i] == v then break end
i = i + 1
end
Run Code Online (Sandbox Code Playgroud)
通常,这些是我们使用这些语句的地方,因为它们之后的任何其他语句都无法访问.但是,有时候,在块的中间写一个返回(或中断)可能是有用的; 例如,如果您正在调试一个函数并希望避免它的执行.在这种情况下,您可以在语句周围使用显式的do块:
function foo ()
return --<< SYNTAX ERROR
-- `return' is the last statement in the next block
do return end -- OK
... -- statements not reached
end
Run Code Online (Sandbox Code Playgroud)
小智 6
在 lua 5.2.0-beta-rc1+ 中,您可以在代码末尾添加一个名为的标签::exit::或类似的内容,然后每当您需要退出程序时只需这样调用它:
goto exit
Run Code Online (Sandbox Code Playgroud)