How do you construct a read-write pipe with lua?

后端 未结 7 1049
甜味超标
甜味超标 2020-12-09 04:26

I\'d like to do the equivalent of:

foo=$(echo \"$foo\"|someprogram)

within lua -- ie, I\'ve got a variable containing a bunch of text, and

相关标签:
7条回答
  • 2020-12-09 05:17

    Here is how I solved the problem, it require lua posix.

              p = require 'posix'
              local r,w = p.pipe()
              local r1,w1 = p.pipe()
              local cpid = p.fork()
              if cpid == 0 then -- child reads from pipe                                     
                 w:close()
                 r1:close()
                 p.dup(r, io.stdin)
                 p.dup(w1 ,io.stdout)
                 p.exec('./myProgram')
                 r:close()
                 w1:close()
                 p._exit(0)
              else -- parent writes to pipe                                                  
                 IN = r1
                 OUT = w
              end
    

    During myProgram execution, you'l read and write from normal io and after this part of code you just have to write/read on IN and OUT to comunicate with child program.

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