ms2008
ms2008

Reputation: 362

lua os.execute() return wrong value

have a problem:

 local stat = assert(os.execute("/usr/bin/pgrep -f 'tail -F /opt/aaa' >& /dev/null"))
 print(stat)  --> 0

But when I type pgrep -f 'tail -F /opt/aaa' >& /dev/null in bash, and then call echo $? it returns 1. Has anybody encountered this before, or know the reason why ;-) what happened?

Upvotes: 3

Views: 1721

Answers (1)

guilespi
guilespi

Reputation: 4702

Doesn't seem to be a Lua problem to me, os.execute is just wrapping a call to system:

 static int os_execute (lua_State *L) {
    lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
    return 1;
 }

If you try the C alternative you have the correct result code?

 #include <stdio.h>
 #include <string.h>

 int main ()
 {
    char command[100];
    int result;

    strcpy( command, "/usr/bin/pgrep -f 'tail -F /opt/aaa' >& /dev/null" );
    result = system(command);

    return(0);
  } 

Upvotes: 3

Related Questions