How to exit a Lua script's execution?
Asked Answered
V

5

50

I want to exit the execution of a Lua script on some condition. For example:

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

Here I want that if I am not getting content my script suppose to terminate is should not go to check to next.

Villanelle answered 25/11, 2013 at 9:22 Comment(1)
os.exit() does just that.Leyte
S
17

extract from the lua api doc :

For syntactic reasons, a break or return can appear only as the last statement of a block (in other words, as the last statement in your chunk or just before an end, an else, or an until). For instance, in the next example, break is the last statement of the then block.

local i = 1
while a[i] do
  if a[i] == v then break end
  i = i + 1
end

Usually, these are the places where we use these statements, because any other statement following them is unreachable. Sometimes, however, it may be useful to write a return (or a break) in the middle of a block; for instance, if you are debugging a function and want to avoid its execution. In such cases, you can use an explicit do block around the statement:

function foo ()
  return          --<< SYNTAX ERROR
  -- `return' is the last statement in the next block
  do return end   -- OK
  ...             -- statements not reached
end
Suit answered 25/11, 2013 at 9:55 Comment(0)
S
66

Use os.exit() or just return from some "main" function if your script is embedded.

Sere answered 25/11, 2013 at 9:28 Comment(0)
T
27
os.exit()

kill process by sending a signal

do return end

stop execution

The two methods are not equal if you want to write and execute some luacode in the interpreter after stopping the execution by launching your program using the -i flag.

th -i main.lua
Trickle answered 26/11, 2013 at 13:51 Comment(1)
In what way are they not equal?Nessy
S
17

extract from the lua api doc :

For syntactic reasons, a break or return can appear only as the last statement of a block (in other words, as the last statement in your chunk or just before an end, an else, or an until). For instance, in the next example, break is the last statement of the then block.

local i = 1
while a[i] do
  if a[i] == v then break end
  i = i + 1
end

Usually, these are the places where we use these statements, because any other statement following them is unreachable. Sometimes, however, it may be useful to write a return (or a break) in the middle of a block; for instance, if you are debugging a function and want to avoid its execution. In such cases, you can use an explicit do block around the statement:

function foo ()
  return          --<< SYNTAX ERROR
  -- `return' is the last statement in the next block
  do return end   -- OK
  ...             -- statements not reached
end
Suit answered 25/11, 2013 at 9:55 Comment(0)
L
7

In lua 5.2.0-beta-rc1+, you can add a label at the end of your code called ::exit:: or something of the like, and then whenever you need to exit the program just call it like this:

goto exit
Leupold answered 7/6, 2016 at 21:5 Comment(3)
Not sure what do you mean by adding a label of ::exit:: , is that a typo ? I don't think it's a correct syntax of lua.Leddy
It is valid Lua syntaxTobar
Ok, that is for the newest lua. I tried on old lua did not workFreestone
A
0

os.exit(ERROR_CODE) e.g. os.exit(1) is an answer.

Below is my practical example working with MS OS:

require("config_fonts")

local success, config_fonts = pcall(require, "config_fonts")
if not success then
    print("Error loading config_fonts.lua:", config_fonts)
    os.exit(1)
end

In a .bat or .cmd file:

%LUA% lua\some_script.lua
if errorlevel 1 (
    echo [ERROR] Failed to process something
    goto eof )
Afterbrain answered 16/7, 2024 at 2:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.