mcbot/libs/shcmd.lua
author Mikael Berthe <mikael@lilotux.net>
Sat, 24 Apr 2010 10:45:00 +0200
changeset 39 237af42156a1
parent 3 508108deee63
child 66 d9c00a9fe9d5
permissions -rw-r--r--
Add checks after shcmd() calls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
function shcmd (cmd)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
    if not cmd or cmd == "" then return nil, "No command" end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
    local fullcmd = cmd.." 2> /dev/null"
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
    local fh = io.popen(fullcmd)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
    result = fh:read("*a")  -- read cmd output
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
    fh:close()
39
237af42156a1 Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents: 3
diff changeset
     9
    if not result then return nil end
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
    -- Trim trailing newlines
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
    local r = result:gsub("\n+$", "")
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
    return r
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
end