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

后端 未结 7 1047
甜味超标
甜味超标 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 04:53

    There is nothing in the Lua standard library to allow this.

    Here is an in-depth exploration of the difficulties of doing bidirectional communication properly, and a proposed solution:

    if possible, redirect one end of the stream (input or output) to a file. I.e.:

    fp = io.popen("foo >/tmp/unique", "w")
    fp:write(anything)
    fp:close()
    fp = io.open("/tmp/unique")
    x = read("*a")
    fp:close()
    

    You may be interested in this extension which adds functions to the os and io namespaces to make bidirectional communication with a subprocess possible.

提交回复
热议问题