什么 lua 功能可以用作 python 或 tcl expect 中的 pexpect? [英] What lua feature can be used as pexpect in python or tcl expect?

查看:26
本文介绍了什么 lua 功能可以用作 python 或 tcl expect 中的 pexpect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 shell 脚本,它们将一些消息打印到标准输出,如果输出与某些正则表达式匹配,我想产生一些其他进程,我可能还想挂起 shell 一段时间,直到另一个进程给出一些反馈.我的要求基本上就像 shell 和 tcl 中的管道一样,或者 python 中的 pexpect,我的问题是,lua 是否提供这样的功能?

我考虑了一个小 lua 的协程,但它在执行 shell 脚本的过程中无法产生,所以我没有挖掘.

解决方案

遗憾的是,Lua 没有提供开箱即用的管道支持,因此您必须在读取和写入之间做出选择.您可以获得的最接近的方法是遍历 :lines() 的一个 io.popen()ed 进程:

for line in io.popen('/some/other/process'):lines() do-- 前一行将阻塞,直到输出可用if line:match '^some regex' then-- 匹配找到!做一些事情结尾结束——达到EOF

如果您可以访问 C 模块,luaposix 会提供一个到 pipe()<的接口/code> 通过posix.pipe()

但是,请记住,Lua 可能不是最适合这项工作的工具.恕我直言,您最好使用 TCL 或 Python,甚至是 bash 脚本.

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.

解决方案

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.

这篇关于什么 lua 功能可以用作 python 或 tcl expect 中的 pexpect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆