繁体   English   中英

如何在 Linux 上使用 Lua、C 的返回值?

[英]How to use return value at Lua, C on Linux?

我检查了以下 URL: Lua os.execute return value

我用C了一个程序,它返回 111 或 222。这是我的代码的一部分。

if (stat == NULL)
{
    system("echo \"stat is NULL\"");
    return 111;
}
else
{
    system("echo \"stat is NOT NULL\"");
    return 222;
}

当我像这样在 Linux 上运行它时, ~/c-program; echo $? ~/c-program; echo $? ,它打印

stat is NULL
111

或者,

stat is NOT NULL
222

在我的终端。

或者像这样,

~/c-program
echo $?

它的打印方式也与~/c-program; echo $? ~/c-program; echo $?

我需要通过Lua运行该程序。 这是我的 lua 脚本的一部分。

local foo = io.popen(~/c-program; echo $?)
local bar = foo:read("*a")
foo:close()

if (tonumber(bar) == 111) then
    os.execute("echo 111")
elseif (tonumber(bar) == 222) then
    os.execute("echo 222")
else
    os.execute("echo \"something is wrong\"")
    os.execute("echo "..bar)
end

像这样打印

something is wrong

即使它有一个打印bar值的脚本,它也不会打印。 我认为 `os.execute("echo "..bar)``` 语法是错误的,但事实并非如此。

我在https://www.lua.org/cgi-bin/demo试过这样

local bar = 111

if (tonumber(bar) == 111) then
    print("bar is "..bar)
elseif (tonumber(bar) == 222) then
    print("bar is "..bar)
else
    print("something is wrong")
    print("bar is "..bar)
end

它打印bar is 111 如果 bar 的值为 333,它也会打印something is wrong

那么,我应该如何使用该 c 程序的返回值作为Lua的变量?

一个程序可以以两种不同的方式退出......

  1. 类似 0 的退出代码(在 Linux 上,这意味着:OK - 无错误)
  2. 文本输出...
    a) 正常:标准输出
    b) 错误:标准错误

使用 Lua 您可以生成无错误的退出代码: os.exit(0)
还有一个错误: os.exit(1)

此外,与其他语言一样,通常使用echo或/和print来生成动态 output 即像 XML/HTML/CSS/Javascript 代码。
在 Lua Standalone 中返回错误代码 1 的示例:

$ lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> rc = 1
> if rc ~= 0 then
>> io.output(io.stderr)
>> io.write('ERROR - EXIT\n')
>> os.exit(rc)
>> end
ERROR - EXIT
$ echo $?
1
$

...所以如果出现错误,stderr 用于输出错误文本,退出代码 1 可用作进一步处理的条件(在 bash/shellscript 中)。

一些关于退出命令和代码的阅读...
https://linuxize.com/post/bash-exit/

现在让我们检查一下 os.execute() 返回的内容...

> os.execute('echo "Hello World"')
Hello World
true    exit    0

os.execute() 返回三个值,而您想要第三个。

然后,您可以通过使用 os.execute() 定义三个本地变量并仅使用最后一个来获取它...

local one, two, three = os.execute('~/c-program')
print(three)
return three

local foo = io.popen(~/c-program; echo $?)看起来不对: popen需要一个字符串,我不知道 Lua 解析器会是什么样子。

如果它确实有效,它将给出"stat is NULL""stat is not NULL" ,然后是 C 程序的退出代码。 从中获取数字会很棘手,因为tonumber不会跳过非数字文本来查找。 直接获取退出代码会更简单:

首先,跳过echo $? 命令的一部分,因此 shell 调用将只运行 C 程序,仅此而已。

如果 Lua 代码不需要 output "stat is NULL""stat is not NULL" ,请使用os.execute而不是io.popen

os.execute的文档说

如果命令成功终止,它的第一个结果为true ,否则fail 在第一个结果之后,function 返回一个字符串加上一个数字,如下所示:

  • "exit" : 命令正常终止; 下面的数字是命令的退出状态。
  • "signal" :命令被信号终止; 下面的数字是终止命令的信号。

file:close()的文档说

关闭使用io.popen创建的文件句柄时, file:close返回与os.execute返回的值相同的值。

所以你应该能够做任何一个

-- Don't need the output text, and allow c-program to print it.
local ok, how, code = os.execute("~/c-program")

-- OR, Don't need the output text, and make sure it's not printed anywhere.
local ok, how, code = os.execute("~/c-program >/dev/null")

-- OR, Need the output text within Lua
local foo = io.popen("~/c-program")
do_stuff(foo)
local ok, how, code = foo:close()

其次是

if how == "exit" then
    if code == 111 then
        print("Got 111")
    elseif code == 222 then
        print("Got 222")
    else
        print("Exited with code " .. code)
    end
elseif how == "signal" then
    print("Stopped by signal " .. code)
else
    print("What happened?")
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM