lua-users home
lua-l archive

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


On Wed, Jul 7, 2010 at 4:58 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
> But what if you want to insert some Lua code in between? Granted, you
> may code a separate Lua program — but in Bash you don't have to do this.
>
>    -exec "ls /dev" (function(input) <do something and echo results>
> end) "grep ^sg.*"

I was going to suggest functionally like

  local function f(ih, oh)
    for line in ih:lines(true) do oh:write(line, line) end
  end
  local path = "/dev"
  local t = sh.chain(
    sh.command("ls " .. sh.Q(path)),  --a.k.a. sh.command.ls(path)
    sh.filter(f),
    sh.command('grep ^sg.*'),
    sh.totable
  )

but allowing a convenience syntax like

  local t = sh "ls $.Q($path) | $.filter $f | grep ^sg.* | $.totable"

giving the impression that Lua functions are executed just like shell
commands but in effect those are really pseudo commands executed
in-process (sort-of like how sh has built-in commands).

However, what about simply this?

  exec 'ls /dev | lua -e $Q(for line in io.stdin:lines(true) do
io.stdout:write(line,line) end) | grep ^sg.*'

Other possibilities include the script passed to "lua -e" opening up a
socket (lsocket) to the main script and filtering its I/O through
that.  That may be another way to implement the bidirectional pipe in
a cross platform way.