[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua as replacement for Bash scripting
- From: Hisham <hisham.hm@...>
- Date: Tue, 6 Jul 2010 14:10:14 -0300
On Tue, Jul 6, 2010 at 6:52 AM, Jerome Vuarand <firstname.lastname@example.org> wrote:
> 2010/7/6 Alexander Gladysh <email@example.com>:
>>> What you can do is introduce some helper code to run external
>>> utilities more easily. For example you could put a __index hook on _G
>>> (or a module), that would check if a command line utility with the
>>> given key exist in the path, and would wrap a call to io.popen in a
>>> Lua function, with proper whitespace escaping. For example you could
>>> then write :
>>> local files = find('.', '-name' '*foo*')
>>> for _,file in files:match('[^\n]+') do
>>> Â Âprint(stat('%n is a %F', file))
>>> Where find and stat are wrappers for the command line tools.
>> That's a nice idea!
>> I would put such hook into a separate table though â to avoid name clashes.
That seems to be what Peter Odding's shell.lua does.
>> Also, how to do piping nicely with this approach?
> Instead of calling the function directly, the wrapper could return a
> "command" object, that you would have to execute ([[ find('.',
> '-name', '*foo*') ]] -> [[ find('.', '-name', '*foo*')() ]]. And you
> could put a piping operator in there to chain commands. For example :
> local files = (shell.find('.', '-size', 0) .. shell.grep('*foo*'))()
> for _,file in files:match('[^\n]+') do
> Â Âprint(shell.stat('%n is a %F', file)())
> You could also feed some string to the command or command chain,
> passed in the last parenthesis pair, that would be inserted as stdin :
> local output = ( commandA .. commandB .. commandC ) ( input )
> Would be roughly equivalent to :
> cat input | commandA | commandB | commandC > output
I've been following this thread with interest, and it seems some stuff
from the luarocks.fs modules serve similar purposes to what has been
discussed here. Perhaps it would be a good idea to join these efforts
and produce a shell utilities module (the LuaRocks modules try to use
stuff such as LuaFileSystem when available or command-line tools as a