Shell Scripts

Ovid on 2004-01-15T17:31:02

I've been programming Perl for years, but am pretty much "just a programmer". I don't know much about shell scripts or working directly with the operating system, but recently when Schwern posted a small shell script for building a simple project shell without h2xs, I started playing with it. After it got to 100+ lines, I had a full-blown project building tool that created stub modules, set up tests the way I wanted them, added all of my helper utilities and optionally checked things into CVS.

Then I started converting it to Perl, because no one uses shell scripts anymore, right?

I took it as an article of faith that shell scripts weren't necessary, but I've quickly discovered that, if I am willing to sacrifice portability, shell scripts are shorter and easier than Perl. Now I'm creating them at work for small tasks and, if I knew sed or awk, I could possibly even skip the nasty parts where I fire up a Perl interpreter for heavy data munging. More stuff to learn, more stuff to learn, more stuff to ...


Hmmm

jordan on 2004-01-15T18:19:35

I'm not much of a shell programmer, not much of a Perl programmer for that matter, but this seems counter to my experience.

Whenever I write shell scripts, I always miss the ease of manipulating filenames and dates and times that I get in Perl. While I can see how you can save a few keystrokes performing some tasks in shell over Perl, like copying files and the like, I don't see any big wins in brevity with shell over Perl.

Also, I believe, although I've not actually checked, that Perl startup is not that much worse than awk or sed. There are some small number of awk or sed startups that are larger than a single Perl startup.

Some things lend themselves to shell scripts, sure, but in my admittedly limited experience, not many.

shell scripts

gav on 2004-01-15T19:32:28

I try to limit myself to shell scripts under 10 or so lines. I've found that if I want to do more it's better having it re-written in Perl. Otherwise you end up with big scary shell scripts.

Re:shell scripts

Mr. Muskrat on 2004-01-16T08:33:10

10?!?? If I hit 5 lines, I start looking to Perl. If it's less that 5 lines, I generally try to refine it to the point that it will work as an alias.

Re:shell scripts

Dom2 on 2004-01-17T09:07:10

If you use zsh, you can get quite a lot done in those 5 lines. Of course, it makes obfuscated perl look like a model of sanity.

-Dom

Shell is lovely but Perl is better

oneiron on 2004-01-16T08:57:43

Bourne shell is lovely. However, as someone who has written a lot of shell and Perl over the years, I use shell only for very short scripts indeed -- or when the script must run on a system that may not have Perl installed.

Many times I've started with a shell script, seen it grow to 50 lines, then 100, then ... only to have to rewrite the damned thing in Perl. I could have saved a lot of time and trouble by writing it in Perl in the first place.

As for shell being shorter, I've found that not to be the case ... once you've written a nice library of Perl functions to perform common shell-like tasks.

I look forward to interest to Tim Maher's upcoming book to see his take on this.