On Mon, Jul 5, 2010 at 12:48 AM, Alexander Gladysh
<agladysh@gmail.com> wrote:
Hi, list!
(Sorry for rather long post, I'm in a writing mood tonight.)
After wasting two hours playing with advanced Bash syntax again, I've
decided that I'm fed up with Bash scripting. Therefore, my Monday
resolution:
Starting this Monday if I write a "shell-script" kind of program that
is more than seven lines long, I'll try very hard to write it in Lua,
not in Bash.
Someone would say that Python or Ruby or Perl would be more
appropriate. Perhaps. I'll even make an amendment to my resolution: If
writing some particular script in Lua would be too frustrating (due to
lack of some LuaRocks-installable library, I guess), I allow myself to
write that particular script in a third language. :-)
Nevertheless, I do love Lua, and I do want to use it even more :-)
I even started to practice. (Of course, I wrote utility scripts in Lua
before, but usually they were less oriented on work with the system
than the stuff that I would write in Bash.)
Here is a script that creates and configures a shared bare Git
repository with hook to send commit notifications to mailing list:
http://github.com/agladysh/misc/blob/master/git/new-shared-git-repo
What this script does is actually irrelevant (it just happened that
needed this functionality).
I wrote the script in the same mode as I write utility shell scripts:
get the results now, don't care if code is ugly.
However, I do not like that I've got as a result (not surprising,
actually). The main problem: it too verbose to be readable.
In the equivalent Bash script I would immediately see what is going on.
The main reason for the unreadability is that I intentionally did not
introduce any abstractions in the code — I wanted to see how the "raw"
code would like. After all, there are not a lot of abstractions
introduced in the simpler Bash scripts as well. Of course, Bash
initially contains most of abstractions necessary (at least low-level
ones).
So, the question is — how to introduce such abstractions to Lua (not
as the language changes, of course, but as a specialized module).
I see, of course, how to refactor this specific script to make
readable. I may even derive some more generic abstractions from such
refactoring.
But I want to think more about module design and I need more
information. (I do not promise that I would write such module anytime
soon though.)
I never paid close attention to the relevant Lua modules, so, perhaps,
there is one already, that would suit my tastes.
Do you know any?
Do you have any comments on possible design for the module?
Do you have (or know) any "shell-script"-like utility code in Lua that
you want to show? (It not necessary should be good — bad code can be
as educational sometimes.)
Any input on the topic is welcome.
Alexander.
P.S. By the way, I want to share a couple of observations for the Lua
suitability as a language for such scripts:
1. Verbose syntax of Lua is not helping with "quick and dirty"
approach. That is probably a good thing. :-)
2. Expansion of local variables in strings could be very useful.
Global variable expansion is done trivially, of course. So, perhaps,
use global variables in such code is justified (but that would inhibit
functional decomposition in the scripts).
3. Poor Lua standard library support for Posix is not helping either.
So, one is almost forced to install additional modules to write
"shell-script"-like code in Lua. This is also bearable. :-)