【问题标题】:lua os.execute() return wrong valuelua os.execute() 返回错误值
【发布时间】:2013-10-26 11:09:12
【问题描述】:

有问题:

 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。以前有没有人遇到过这种情况,或者知道原因 ;-) 发生了什么?

【问题讨论】:

  • 你使用的是哪个 Lua 版本?
  • 5.1.4 在 RHEL5_x86_64 平台上
  • 在 Lua 5.2 中你会得到状态。见lua.org/manual/5.2/manual.html#pdf-os.execute
  • @lhf 我在 5.2.2 下尝试过这个,但它也没有返回正确的值。它返回trueexit
  • 退出后有一个数字退出代码。

标签: lua


【解决方案1】:

对我来说似乎不是 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);
  } 

【讨论】:

猜你喜欢
  • 2012-03-29
  • 2010-09-13
  • 2012-12-13
  • 2012-10-27
  • 2011-08-27
  • 2018-05-26
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
相关资源
最近更新 更多