Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Lua Cant capture all output from io.popen

dearikomaru

New Coder
Code:
Lua:
function func.execute(command)
    local f = io.popen(command .. ' 2>&1 && echo " $?"')
    local output = assert(f:read "*a")
    local tempOutput = output
    local begin, _, code = output:find " (%d+)\n$"
    if begin == nil or begin == '' then
        output = table.concat(func.strSplit('\n', tempOutput), "|.|")
    else
        output, code = output:sub(1, begin - 1), tonumber(code)
    end
    return code == 0 and true or false, code, output
end
From the code above, if I run the command "cd somewhere && cat file". Condition that occurs if the first command fails (directory not found) then the resulting output is an empty string. And if all fails, then the output that is caught is only the last command (if there are 3 commands then only the 3rd output is caught). I want if the message failed that appears caught all. For example from this command "cd somewhere && cat file" and then the output is "bash: cd: somewhere: No such file or directory\n" like that. Please help me. thank you.
 

New Threads

Buy us a coffee!

Back
Top Bottom