检查 Lua 中 shell 命令的输出是否为空



仅当pgrep -x foo的输出为空时,我才尝试激活一些Lua行。我尝试了以下方法:

if os.execute("pgrep -x foo") then
   -- My lines here
end

但是,这似乎不是正确的解决方案,即使在语法上也可以。

local result = os.execute("pgrep -x foo")
if result ~= true and result ~= 0 then
   -- My lines here (no such process)
end

也许尝试检查nil

if os.execute('pgrep -x foo') == nil then
    print('empty')
end

如果您不希望匹配"精确",请删除-x选项。

最新更新