# In Defense of Shell Scripts - **期号**: SRE Weekly Issue #416(2024-03-17) - **作者**: Amin Astaneh — Certo Modo - **链接**: https://certomodo.substack.com/p/in-defense-of-shell-scripts ## 简介 Sometimes, shell is just the right tool for the job. ## 正文 ![Article Blog Image Article Blog Image](https://substackcdn.com/image/fetch/$s_!9aoT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ce65fb1-23cf-4501-aacb-48855a4b41a3_990x607.jpeg) (cross-posted from [certomodo.io](https://certomodo.io/best-practices/in-defense-of-shell-scripts.html)) Throughout my career, I’ve been known as the engineer who solves a LOT of problems using shell scripts. With it, I’ve built: - a tool that creates and destroys test services at Meta to help new engineers learn how to use the [company’s planetary-scale CD system](https://www.usenix.org/system/files/osdi23-grubic.pdf) - a parallel distributed shell that integrates with a company’s asset management system to generate the list of hosts to connect to - plugins for an ‘Ops API’ that enables an engineer to approve and automatically apply requested infrastructure changes without the need for manual effort Usually, when I write a shell script and submit it for review, I tend to get either of these responses from my peers: - “ **Wow** , this is really neat! This should save us a lot of time.” - “ **Eww, gross!** Why didn’t you use an actual programming language?” Let’s talk about when shell scripts are the more compelling solution. ### When CLI tools are available, and APIs aren’t In some situations, the data sources/services you need to accomplish your task are best accessible using CLI tools. In that case, implement your solution in shell script and just run the CLI tools directly. That will be more efficient than using some kind of system library to execute them and writing all the boilerplate to supply arguments and capture stdout, stderr, and exit codes. ### When prototyping If the 80% solution can be implemented in shell script in an hour using `curl` and `jq` and the 100% solution can be implemented in Python in 8 hours using `requests`, do the shell script version first to validate the idea! ### When authoring a CLI tool **CLI tools need to be fast!** It’s so frustrating to run one where it takes over a second to just load library dependencies (Ruby/Rubygems, I’m looking at you!). The exception to this is if you’re already proficient in compiled languages (eg: Golang, Rust), which are great for building CLI tools. ### When the shell script solution is significantly smaller Consider this script that checks to see if my website is up: `curl -o /dev/null -s -w "%{http_code}" https://certomodo.io | grep -q 200` That one line of code is equivalent to this much larger Python script: ``` import requests def check_url(url): try: response = requests.get(url) return response.status_code == 200 except requests.RequestException: return False url = "https://certomodo.io" check_url(url)) ``` Tiny shell scripts won’t need the QA burden of larger projects such as build, lint, and unit tests (though tools like [shellcheck](https://www.shellcheck.net/) and [bats](https://github.com/bats-core/bats-core) do exist)! ### Conclusion I end this article with the following quote: **“And who better understands the Unix-nature: he who writes the ten thousand lines, or he who, perceiving the emptiness of the task, gains merit by not coding?”** - [Master Foo](http://www.catb.org/~esr/writings/unix-koans/ten-thousand.html) ![img img](https://substackcdn.com/image/fetch/$s_!Cp8q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcf53f56-0298-4d83-aaeb-e2e7e2fadc7e_598x426.jpeg)