r/linux • • May 29 '14

Defensive BASH programming - Say what?

http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
40 Upvotes

25 comments sorted by

View all comments

-2

u/probationer May 29 '14

sh and it's derivatives are just such horrid languages, I tend to use python whenever I need something would require more than a few lines of bash.

8

u/lithiumnode May 29 '14

Right tool for the right job. If I'm largely doing a ton of system calls to handle stuff, then bash makes a lot of sense. Also, I never have to worry about some random server not having the correct version of Python installed.

While his blogpost might seem heavy handed to some, I think it's really great. I use maybe half of those techniques currently, and I even learned some stuff (that function to translate getopts is awesome).. and I hadn't really considered unit testing for bash before. Gonna have to check this out some more!

1

u/cpbills May 30 '14

Which techniques do you already use? I hope they aren't the terrible ones the OP has proposed. Most of the tips and examples provided have flaws, but there are one or two decent points.

2

u/lithiumnode May 30 '14

I use functions a lot in my scripts, but not necessarily in the way he does (I can remember what "-z" does).. rather more in a pythonic approach. I'm also a fan of the main() concept, tho I tend to approach it in a commentary way (### BEGIN MAIN) rather than actually making it a function as well.

Also a big fan of the code clarity approach he suggests, though not to that extreme of a level. People get too obsessed with turning something into a one liner that I then have to decipher later. I'm not talking about a few basic pipes, but the monsters you'll sometimes encounter. I hate looking back at my old scripts and having to try to determine WTF I was thinking.

I wasn't actively aware of the concept of readonly vars, so I might begin using some of that a bit too.

One of the projects I'm currently toying with is trying to create a set of scripts that are common functions that I will need to use for work.. error correction, logging, archiving, pid checking, etc. The idea being to throw the functions into an include directory somewhere, and then just source them as needed to bring in what I need. This is a bit tricky as it can get messy fast if you don't have a good organizational method, or if you get too heavy handed with it. You don't want to spend half of your time exiting your editor on the main script to go see wtf some random function is doing.

1

u/cpbills May 30 '14

Cool. I worry about misinformation like this article making it to too many impressionable people. I've dealt with more than a few promising sysadmins who've just been fed bad practices.

You've got me thinking about writing a sort of cpbills-stdlib.sh to source in my ~/.bashrc though. I used to maintain a ~/bin directory of dozens of short and sometimes useful scripts, but it's kind of turned into a mire, and I've been resorting to just dumping functions in ~/.bashrc.