What lua feature can be used as pexpect in python or tcl expect?

前端 未结 1 680
清歌不尽
清歌不尽 2021-01-21 23:46

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 wh

相关标签:
1条回答
  • 2021-01-22 00:09

    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.

    0 讨论(0)
提交回复
热议问题