So I’ve found myself writing lots of bash scripts
recently and because they tend to do real things to the file system or cloud services they’re hard to test… it’s painful.
So it turns out there is an awesome linter/checker for bash
called shellcheck
which you can use to catch a lot of those gotchas before they become a problem.
There is a great plugin for vscode
so you get instant feedback when you do something you shouldn’t.
Better still it’s easy to get running in your build pipeline to keep everyone honest. Here is an example task for Azure Devops to run it on all scripts in the ./scripts
folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
– bash: | | |
echo "This checks for formatting and common bash errors. See wiki for error details and ignore options: https://github.com/koalaman/shellcheck/wiki/SC1000" | |
export scversion="stable" | |
wget -qO- "https://storage.googleapis.com/shellcheck/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv | |
sudo mv "shellcheck-${scversion}/shellcheck" /usr/bin/ | |
rm -r "shellcheck-${scversion}" | |
shellcheck ./scripts/*.sh | |
displayName: "Validate Scripts: Shellcheck" |
Next on my list is to play with the xunit
inspired testing framework for bash
called shunit2 but kinda feel if you have enough stuff to need tests you should probably be using python
.