Lua 如何判断 Shell 命令的输出是否为空

我想在 Lua 中,仅当 pgrep -x foo 命令的输出为空时才执行一些 Lua 代码。我已经尝试过以下代码:

if os.execute("pgrep -x foo") then
   -- 在这里编写代码
end

不过,这似乎不是正确的解决方案,即使从语法上讲是正确的。

原文链接 https://stackoverflow.com/questions/41914156

点赞
stackoverflow用户499581
stackoverflow用户499581

也许尝试检查 nil

如果 os.execute('pgrep -x foo') == nil then
    print('空的')
end

如果你不想匹配是"exact"的,那么就移除 -x 这个选项。

2017-01-28 20:39:06
stackoverflow用户1847592
stackoverflow用户1847592
```lua
local result = os.execute("pgrep -x foo")
if result ~= true and result ~= 0 then
   -- My lines here (no such process)
end
本地结果等于执行"pgrep -x foo"
如果结果不等于true或者不等于0,则
--我的代码(没有这个进程)
2017-01-28 21:33:22