繁体   English   中英

检查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

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

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选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM