Bash Scripting: Why didn't I start this earlier?
burakkanber.com
burakkanber.com
Can someone please provide some arguments why I should write bash scripts instead of Python scripts?
There are a lot of best-practices that one need to adapt though, and there's definitively a learning-curve. Generally scripts should work under things like dash, and not be bash-specific. That means delegating some work to sed/grep/awk (or being really "clever").
I've found that using functions, and passing arguments rather than setting (global) variables help a lot -- and if you get to the point that you really want do debug, you're better off rewriting in something like python.
I've never liked perl -- it's always seemed like a bit of a pointless exercise in moving side-ways rather than "up". I've never had to move away from shell scripts out of performance needs, only out of maintenance needs -- and for me perl buys me nothing in that direction.
Now, things like fabric[f] can make sense -- but the benefit of (ba)sh is that it's guaranteed to be there (on a nix platform) -- and it is great for bootstrapping.
I'm not* a fan of how some projects use (ba)sh, though. The ones that list instructions like these (and there have been a few lately):
curl http(s)://great-tool.com/install | sudo bash
They often a) don't use https and b) require sudo/root blindly (rather than at least wrapping what needs sudo in something like: prompt -p "Need superuser permission for apt-yum-install libfoo [y/n]: " ans
if [ "${ans}" == "y" ]; then
sudo apt-yum-install libfoo
else
echo "Aborted."
exit 1
fi
Which generally works rather painlessly -- and could be further "automated" by allowing a --yes-go-ahead, or checking if the script is running as root.[f]: http://fabfile.org
All useful tools grow.
My experiences with job control in Python were admittedly not quite as pleasant.
Use a modern scripting language like Python or Ruby otherwise you are entering a world of pain.
In contrast, a bash program calls out to lots of little programs: sed, awk, grep, etc. Different platforms have different implementations which take different flags, etc.
My friend and I just spent a couple of days getting some bash scripts that ran fine under Cygwin to also run on OSX. It was painful.
Pash sadly isn't complete enough to be used, though.