Don't write shell script. Not in bash, and not in any other shell. There are two very simple reasons for this:
- They are not portable.
- They are hard to read.
Nowadays, we have a much better option - write those same shell-scripts in a cross-platform scripting language, such as Python or Perl. These languages are much easier to read than some devilish combination of awk, sed and a cryptic syntax. Well, Python is. But Perl can be readable too, contrary to common wisdom, if a bit of care is taken when writing the script. Sure, most shell scripts use some really neat tricks & hacks, but let me quote
Brian Kernighan:
"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
Brian Kernighan
(If you follow the link, you'd see Mr. Kernighan has a hand or two in the scripting pot himself. Oh well...)
In my experience, even the most temporary script ends up expanding beyond its original intention, and thus needs to be modified and maintained. Shell scripts are just not the right tools anymore.
Further Reading
- Python, Subprocess and Multiple Arguments
- Programming with VI
I'd tend to agree, although portability is more of an issue for me than readability. I traverse between Linux and Windows-land quite a lot, and I'd be amongst the first to appreciate the value of a cross platform scripting language.
ReplyDeleteGrep, sed, awk and find are my dear friends though, and I can't say I can bear to leave them behind for too long.
I agree that portability is very important. I work on Windows, Linux and my beloved Mac every day. There really is no reason not to have cross-platform applications this day & age.
ReplyDeleteI use grep on the shell every day, but sed and awk mostly in scripts and makefiles, which is why I wrote this post. Anything they do, you can do in Perl in almost the same effort. Python takes a bit more typing, but the readability is worth it, in my humble opinion.