i've checked following URL: Lua os.execute return value
I've made a program with C
which returns 111 or 222.
Here is part of my code.
if (stat == NULL)
{
system("echo \"stat is NULL\"");
return 111;
}
else
{
system("echo \"stat is NOT NULL\"");
return 222;
}
when i run this at Linux like this, ~/c-program; echo $?
, it prints
stat is NULL
111
or,
stat is NOT NULL
222
at my terminal.
or like this,
~/c-program
echo $?
it also prints same way like ~/c-program; echo $?
I need to run that program via Lua
. Here is part of my lua script.
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
this prints like this
something is wrong
Even it has a script that prints the value of bar
, it does not print.
I thought that ``os.execute("echo "..bar)``` syntax is wrong, but it's not.
i tried like this at 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
it prints bar is 111
. In case bar's value is 333, it also prints something is wrong
So, how should i do to use that c-program's return value as a Lua
's variable?