simple shell question

I have always been of the view that as soon as a shell script got complicated it was time to move to perl(1), but that just makes me old fashioned these days.
I've heard lots of people say versions of this, and everyone seems to differ on what exactly "complicated" means. Some have a seemingly arbitrary line count of maybe 100 LOC, others suggest when you start using functions or external tools inside the script like awk/sed/perl/etc in an "advanced" way...

I mostly agree that at a certain point it's time to move away from shell to something, but to what? I used to know Perl fairly well, but have only ever worked with two other people who have understood it. I don't enjoy writing Python but has been a bit more common. I detest javascript, but it's been common in some workplaces.
The most commonly "understood" scripting language I've come across in most of my workplaces is shell (plus various external tools like awk/sed/curl/jq)...Admittedly nothing is ever very complicated, and most people I've come across don't know how to write portable and maintainable shell scripts...

I tried to do some "personal fun stuff" in Perl a coupe of years ago and found that CGI had been removed from core and was generally discouraged in favour of some other frameworks. After trying to relearn Perl and one of the frameworks I eventually gave up and wrote some Go for the first time which seemed to come a little more naturally to me now 🤷‍♂️

Sorry, taken this way off topic now!
 
I tried to do some "personal fun stuff" in Perl a coupe of years ago and found that CGI had been removed from core and was generally discouraged in favour of some other frameworks.
I notice with Perl and Python it is best to just ignore notices saying that things are deprecated. Usually it is just another developer on the PIP, CPAN trying to push an agenda.

For example the Git http-backend uses Perl CGI.pm and that is likely going to stay around much longer than whatever CGI replacement Perl is using these days.
 
There once was a time when every dynamic website was built around Perl's CGI modules. Perl is awesome but it can be quite unreadable due to its lax syntax rules. I actually learned to code Perl on my Amiga. Then used it a lot professionally on Windows (Remember Activestate Perl?) to make life as an admin a little easier. A few years later I worked for a large international oil company. None of the available commercial network management products were capable of dealing with the large amount of routers and switches they had. So they wrote their own sofware, in Perl. The team I worked for had to manage and maintain that software. If I screwed up there would be around 2000 network engineers world-wide just sitting on their thumbs not being able to do their work.
 
I notice with Perl and Python it is best to just ignore notices saying that things are deprecated. Usually it is just another developer on the PIP, CPAN trying to push an agenda.

For example the Git http-backend uses Perl CGI.pm and that is likely going to stay around much longer than whatever CGI replacement Perl is using these days.
I wouldn't say they have a specific agenda. It's really more that they fall into herd mentality of jumping on whatever the new and shiny bandwagon is. I see this across disciplines and industries.
In my world (mobile development), everyone on iOS is going on and on about how Swift is the best bla bla bla and C sucks and you should move away from it because it's old (as if that's an actual reason). Similarly, on the Android side, the same thing is happening except with Java and Kotlin. None of them have any deeply-thought agenda. Most of them are just jumping on the bandwagon just because everyone else said it's the hottest thing since sliced bread.
 
I actually learned to code Perl on my Amiga.
I thought Perl was very learnable - I learnt it while on night shifts downloading blackbox signalling logs on trains over a slow serial connection, just reading the Camel Book and doing the exercises in my head.
When I eventually sat down at a computer to do something Perl just flowed really nicely, never had that experience with any other language...
 
I've heard lots of people say versions of this [use perl when shell gets complicated], and everyone seems to differ on what exactly "complicated" means.
I used Stephen Bourne's original shell a lot. So I know what I think all its derivatives and look-alikes should have in common.

My rule has always been to use perl as soon as the job can't be done in strict traditional (and very portable) Bourne shell.

For that reason my shell scripts will generally run on sh, ash, dash, bash, zsh, ksh, etc. That goes for system scripts on Linux because Debian uses dash, while other distributions use bash.

Feeling the need for arrays of any kind is often the first warning. But for scripting I would generally choose perl over shell if the task required:
  • lots of string processing;
  • arrays of any kind;
  • compound data structures (stacks, trees, linked lists, etc.);
  • complex pattern matching;
  • genuine speed; or
  • CGI in a web server (never any shell).
Speed can be a determining factor. Most shells are internally much slower than perl (and bash is an utter pig), but shell scripts also often need to run external processes to get things done (e.g. cut, grep, sed, awk, expr, etc.), where perl does not. Forking and execing one or more process once per loop can get extremely tiresome as the loop count increases. Processing web logs in real time (so you finish before the next one is due) is the classic example.
 
Back
Top