Shell Scripts – a fundamental skill for developers
There’s something of an inside joke among sportscasters who, seeking to say something nice about an otherwise unremarkable player in a top-plays-of-the-day clip, will refer to him as “fundamentally sound”. It’s the athletic variant of the timeless caveat that your blind date has a “nice personality.”
As developers, we should be proud to say we’re fundamentally sound. Here’s a few quick challenges have crossed my desk recently. For each, note which technology you would turn to first.
- You need to quickly determine how an undocumented REST API works
- You need to make a copy of a file for each of the 15 locales that your application supports, naming the new file with the name of the locale it represents (e.g. file_en_GB.txt)
- You need to extract a certain set of links from a page hosted on another site once an hour
If you answered “curl”, “cp”, “grep”, and “cron” – congratulations! You are truly, fundamentally sound.
In a world where we’re surrounded by power tools like Java, .NET, and relational databases, we often forget that the ultimate Swiss Army knife – the shell – can frequently solve problems quicker and easier. Sadly, shell scripting is becoming something of a lost art. But no matter how much snickering I hear when I pull up my green on black xterm, I never regret having that tool in my toolbox.
I’ll be the first to admit that a shell script larger than a few dozen lines is nearly impossible to maintain. Ironically I see this as a good thing. It serves as a safety valve and informs my rule of thumb – if I can do it in less than 50 lines of shell code, I do. If I can’t, I pull out the power tools. It forces me to be focused and think the problem through. You’d be amazed what you can do with 50 lines of crafted shell code. Just don’t forget the comments : )