lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 06.12.2013 16:00, Daurnimator wrote:
On 6 December 2013 09:09, Alexander Schulz <aiqpost@yahoo.com> wrote:
i want to create a script with lua that reads from a pipe and allows to read input from the user terminal.

Example: history -n | tail -n 10 | my_script

In this case is io.stdin a file descriptor to the pipe.
How can i get now in lua a file descriptor on the current terminal?

io.stdin is what you want; you can use the :lines() iterator:

I don't think this is what he OP wants; I think he wants to read from the pipe (stdin) _and_ from the terminal. This can be done in Linux by something like

	history -n | tail -n 10 | my_script 3<&1

The script can then open fd 3 for reading (using, e.g., luaposix) and get user inputs from there. The following example reads a line from the pipe and max. 128 characters from the terminal in turn:

--------------------------------------------------
#!/usr/bin/lua

local P = require "posix"

P.open(3, P.O_RDONLY)

while true do
    local line = io.read()
    if not line then break end
    print("From pipe: '" .. line .. "'")
    local user = P.read(3, 128)
    print("From user: '" .. user .. "'")
    if not user or user == "\n" then break end
end
--------------------------------------------------

Regards,
Bernd

--
http://sudrala.de