lua-users home
lua-l archive

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


On Mon, Apr 23, 2012 at 19:46, Coda Highland <chighland@gmail.com> wrote:
>>>  proc{'find', "/usr", "-name", pattern, "-print0"}.pipe{'xargs', '-0', 'du'}()
>>>
>>>  find /usr -name "$pattern" -print0 | xargs -0 du
>>>
>>>  find /usr -name $pattern -print0 | xargs -0 du
>>
>> My thought would be something a little more verbose, like:
>> proc1 = create_process('find', {path='/usr', name=pattern, print0=true})
>> proc2 = create_process('xargs', {nullsep=true, delim='u'})
>> proc1.stdin = proc2.stdout
>> proc2:start(); proc1:start()
>
> du is the command xargs is invoking; it's "disk usage", not a
> delimiter command to xargs. >.>
>
> That said. None of those are appropriate!
>
> Try this:
>
> for file in path.find{basedir="/usr", name=pattern} do
>  print file, fs.stat(file).size
> end
>
> If you're thinking about it using your bash/POSIX idioms you're not
> taking advantage of the fact that you have a scripting language! Not
> to mention this runs faster and with less memory because you're not
> constructing a list of files and then passing them to a second
> process; path.find can return an iterable that sifts through the
> filesystem incrementally -- and the stat cache can be shared between
> the find process and the size lookups.
>
> And this avoids the obvious question of "what does this snippet do?"
> because the code is more self-documenting in the process!
>
> /s/ Adam
>

Of course that's a much better approach. I just meant to translate the
command invocation itself, not the intended action. (and failed,
apparently)

-- 
Sent from my toaster.