Question

i have some shell scripts those printing some message to stdout, and i want to spawn some other process if output matches some regexp, i may also want to hang the shell for a while untill the other process give some feed back. my requirement basically behaves like pipe in shell and tcl expect, or pexpect in python, and my question is, does lua provide such feature?

i've considered a little lua's coroutine but it cannot yield during the middle of executing a shell script, so i did not dig.

Was it helpful?

Solution

Sadly, Lua doesn't provide piping support out of the box, so you'll have to choose between reading and writing. The closest you can get is by iterating through the :lines() of a io.popen()ed process:

for line in io.popen('/some/other/process'):lines() do
    -- previous line will block until output is available

    if line:match '^some regex' then
        -- match found! do some stuff
    end

end  -- EOF reached

If you have access to C modules, luaposix provides an interface to pipe() throughposix.pipe()

However, bear in mind Lua may not be the most appropriate tool for the job. IMHO you'll be better off using TCL or Python, or even a bash script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top