lua os.execute() 返回了错误的值。

有一个问题:

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

但是当我在 bash 中输入 pgrep -f 'tail -F /opt/aaa' >& /dev/null,然后调用 echo $?,它返回 1。有人遇到过这种情况,或者知道为什么吗?;-) 发生了什么?

点赞
用户1783752
用户1783752

这对我来说似乎不是Lua的问题,os.execute只是在调用system

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

如果你尝试使用C替代方法,你能得到正确的结果吗?

#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);
}
2013-10-26 13:09:08