Programming Language Advice to My Daughter, Part 3: Shell Script *
Classic techniques remain effective.
In this “Programming Language Advice to My Daughter” series:
Part 1: Ruby
Part 2: C++
Part 3: Shell Script *
Part 4: Java
Part 5: Wrap-up and FAQ
A shell script is a computer program designed to be run by the Unix/Linux shell, such as the Bourne Again SHell (Bash).
A Story
Many years ago, I was working on a .NET project that required extracting a column of data from a CSV file. A senior .NET contractor confidently said, 'Easy—I’ll build a .NET application with a graphical user interface that lets the user select a CSV file, perform the extraction, and save the output to a file of their choice.' Several other .NET developers nodded in agreement. He went ahead and spent a few hours building it.
I was shocked to hear and see that because this is just a one-line shell script.
For example, to extract the year column in the following CSV file.
The one-line script is:
awk -F "\"*,\"*" '{print $2}' MySoftware.csv > output.txt
Shell Scripting is still very useful
Some might think “shell scripting is too old school”. Yes, it is old-school but still remains very useful.
Unix is more relevant than before
WSL (Windows Subsystem for Linux) was introduced to Windows OS in 2016. WSL 2 was released in 2019. All major Operating Systems (windows, Linux, macOS) all support Unix.
You may use shell scripting on a Windows computer.
Shell Scripting embraces two important concepts
1. Automation
Shell Scripting, in essence, is automation.
Check out my daughter’s article “Set up Linux Infrastructure using Stackscripts”.
We, as test automation engineers, often need to invoke shell scripts inside our E2E test scripts. Below is an example: invoke a shell script (kill_browser_and_driver_processes.sh
) to clean up browser and driver processes before every build.
2. Pipeline
A pipeline is a mechanism for inter-process communication using message passing. For example, the below shell script gets the number of unique IDs in a CSV file.
% cut -f 1 -d "," enrolments.csv | tail -n+2 $1 | sort | uniq | wc -l
For more, check out my daughter’s article, “Different Approaches to Filtering a CSV file”.
This aligns with good software design principles supported by experienced software engineers: define a set of small, high-quality, purposeful methods and invoke them in a chained manner.
Related reading: