6 Techniques I Use to Create a Great User Experience for Shell Scripts #932
Labels
CLI-UX
Command Line Interface user experience and best practices
shell-script
shell scripting in Bash, ZSH, POSIX etc
shell-tools
Tools and utilities for shell scripting and command line operations
software-engineering
Best practice for software engineering
6 Techniques I Use to Create a Great User Experience for Shell Scripts
Snippet
"Skip to content
Home
Blog
Jason on Twitter
Jason on GitHub
Toggle dark mode
6 Techniques I Use to Create a Great User Experience for Shell Scripts
Sep 11, 2024
"You should go and check out the shell script in the repo because it's very nice. It has colored output, it's super safe... it's really a masterclass in terms of writing shell scripts."
Thank you Gunnar Morling for the shout-out! 😊
In January 2024, I along with a few dozen performance-minded geeks got nerd-sniped into participating in Gunnar's One Billion Row Challenge 1️⃣🐝🏎️ ."
Content
In January 2024, I along with a few dozen performance-minded geeks got nerd-sniped into participating in Gunnar's One Billion Row Challenge 1️⃣🐝🏎️.
Gunnar quickly became overwhelmed being the (unpaid) evaluator of a constant stampede of entries. I jumped in to help him automate the evaluation steps with a shell script and received the above testimonial from Gunnar at his Javazone talk (check it out to hear all about the performance techniques used in the challenge: "# 1BRC–Nerd Sniping the Java Community - Gunnar Morling").
Here are 6 techniques I used in the #1BRC shell script to make it robust, safe and fun for Gunnar to use:
I believe that clear error messages are crucial for a good user experience. That's why I implemented thorough error handling and input validation throughout the script. For example:
This approach helps users quickly identify and resolve issues, saving them time and frustration.
To make the script's output more readable and user-friendly, I used ANSI color codes to highlight important information, warnings, and errors. For instance:
This visual distinction helps users quickly grasp the nature of each message.
I wanted users to understand exactly what the script was doing at each step. To achieve this, I implemented a function that prints each command before executing it:
This matches the output format of Bash's builtin
set -x
tracing, but gives the script author more granular control of what is printed.This level of transparency not only keeps users informed but also aids in debugging if something goes wrong.
I wanted to ensure that the script would exit immediately if there was an error in the script itself, but also allow it to continue running if individual forks encountered issues. To achieve this, I used the Bash options
set -e
andset +e
strategically throughout the script. Here's how I implemented this technique:This approach gives the script author fine-grained control over which errors cause the script to exit and which can be handled in other ways.
Knowing that users might run this script on different operating systems, I added logic to detect the OS and adjust the script's behavior accordingly:
This ensures a consistent experience across different environments. Many #1BRC participants were developing on MacOS while the evaluation machine ran linux for example.
To support multiple benchmark runs without overwriting previous results, I implemented a system of timestamped file outputs. This allows users to run the script multiple times and keep a historical record of all results. Here's how I did it:
By implementing these techniques, I aimed to create a user-friendly, informative, and robust shell script that provides a great experience for users running and analyzing benchmarks. I hope these ideas inspire you to enhance the user experience in your own shell scripts!
I'd love to hear your thoughts on these shell scripting techniques or any other tips you have for creating great user experiences in scripts. Feel free to join the discussion:
Suggested labels
None
The text was updated successfully, but these errors were encountered: