Skip to content

A demonstration of how to improve the code quality of a Bash script

License

Notifications You must be signed in to change notification settings

jometzner/bash-script-code-quality

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bash script code quality

This repository demonstrates via its commit history the process of improving the code quality of a Bash script. The commit messages explain the introduced changes and their motivation while this document lists the used tools and references.

Tools

The ShellSpec framework is used for testing as it employs a behaviour-driven development approach with a domain-specific language that helps create easily readable tests. In addition, it provides many options for mocking and other useful testing features.

The other essential tool to detect error-prone code or even bugs is the static code analyser ShellCheck. It can be run in the command line or integrated into editors.

For example, for the presentation of the process, the ALE plugin and Airline status line for Vim have been used to show the annotations in the editor.

The Flog plugin for Vim has been used to check out the commits one after another, so tests could be run at the various stages.

References

Calling Docker for separate commands

Variables

Initialise and run ShellSpec

Unit testing single-file scripts

Error handling

Writing output

ShellCheck shell declaration

Custom assertion counting occurrences of a string

Match function arguments

Command substitution

Working with calculation results

Define multiline string variables

Call original command instead of mock

Prefer [[ ]] test over [ ] in Bash

Define default value for variable

Problems with looping over find output

Bash Pitfalls – for f in $(ls *.mp3)

Read find output into array

Useless use of cat

Execute command upon script termination

About

A demonstration of how to improve the code quality of a Bash script

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 100.0%