As a Ops/DevOps professional, I have many years of experience writing shell scripts. Probably you’re the same as me, right?
I deeply treasure my achievements with shell. Yes, I still do, even now. But I have to make a decision. Finally! Drop Shell And Embrace Python.
Why is that? Check this blog post and discuss with me.
It’s really hard to reject the temptation of shell. Apparently shell is a very old and widely-used language. But it’s far from modern programming languages. Poor error handling, weird syntax, no package mechanism to reuse the code, the list goes on and on …
However, as you can see, shell scripts are almost everywhere on the internet. It’s really hard to avoid them. So Why?
- Unix PIPE philosophy makes shell the default glue language. It integrate all kinds of (relatively) small tools, solving a bigger problem. So if you check any popular online tutorials now, you will probably see shell scripts somewhere.
- With shell, it’s super easy to get your hands dirty and obtain fuzzy warm feeling of making progress. Each shell command you have issued in the terminal, it’s a reward. It makes you feel that you’re making constant progress. Hard to say no, isn’t?
- No extra wrapper layer. Let’s say, you need to trigger some elasticsearch actions by programming. You can either call elasticsearch management REST API directly by shell, or use elasticsearch Python SDK. If you choose python, and the script is not working. Then what? It could be some bugs in Python SDK or the way you use Python SDK. No doubt people are in favor of shell scripting sometime.
Regardless of all benefits, my DevOps fellows, we have to look ahead. And move on.
I will choose Python as default for all unattended scripts which will run automatically. There are still some room for shell. For example, quickly wrap up some commands, and execute them manually from the terminal.
Netflix is also using Python a lot for their DevOps work.
What drives me to switch from Shell to Python?
First of all, it doesn’t necessarily need to be Python. It could be any modern programming languages which you’re comfortable with. Like Ruby, Golang, etc.
- Reuse code and modules across projects. With years’ accumulation, I come up with a shell common library (Github). With this library, I can easily and quickly accomplish lots of DevOps tasks. e.g, Enforce Pre-check Before Deployment, run deployment and maintanence via Jenkins, etc. However distributing the shell scripts and upgrading them is quite a burden. Now with Python, I can easily pack up the logic as pip package.
- Super hard to make decent and reliable shell scripts. Think you’re shell Ninja? Run Shellcheck against your scripts. And with the absence of error handling, your scripts can easily give you false positives. And consequently it may incur serious damage, or make your trouble shooting difficult.
- Easy requirements may not be easily supported. For example, when you run the scripts, you want to see the output in both terminal and logfiles. With some research, you may finalize with the solution of tee command. However you will have to deal with weird bash issues like this one.
Now my Python journey starts. What about yours?
- ShellCheck: Code Check For Shell Scripts
- Shell Redirect Output To File, And Still Have It On Screen
- Bash -e Doesn’t Exit As I expect