xvlaze
xvlaze

Reputation: 869

Check if output of a shell command is empty in Lua

I am trying to activate some Lua lines only if the output of pgrep -x foo is empty. I have tried the following:

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

However, this doesn't seem to be the correct solution, even syntactically OK.

Upvotes: 1

Views: 578

Answers (2)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23727

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

Upvotes: 3

l'L'l
l'L'l

Reputation: 47169

Maybe try checking for nil:

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

If you don't want the match to be "exact" then remove the -x option.

Upvotes: 1

Related Questions