检查 shell 命令的输出在 Lua 中是否为空
Check if output of a shell command is empty in Lua
我试图仅在 pgrep -x foo
的输出为空时才激活某些 Lua 行。我尝试了以下方法:
if os.execute("pgrep -x foo") then
-- My lines here
end
然而,这似乎不是正确的解决方案,即使在语法上也没有问题。
也许尝试检查 nil
:
if os.execute('pgrep -x foo') == nil then
print('empty')
end
如果您不希望匹配 "exact" 则删除 -x
选项。
local result = os.execute("pgrep -x foo")
if result ~= true and result ~= 0 then
-- My lines here (no such process)
end
我试图仅在 pgrep -x foo
的输出为空时才激活某些 Lua 行。我尝试了以下方法:
if os.execute("pgrep -x foo") then
-- My lines here
end
然而,这似乎不是正确的解决方案,即使在语法上也没有问题。
也许尝试检查 nil
:
if os.execute('pgrep -x foo') == nil then
print('empty')
end
如果您不希望匹配 "exact" 则删除 -x
选项。
local result = os.execute("pgrep -x foo")
if result ~= true and result ~= 0 then
-- My lines here (no such process)
end