From f97634a61e03278ef20022fa0b5a5e417384c05b Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 29 Mar 2025 09:20:39 -0500 Subject: [PATCH] Add Have Script ShellCheck Itself When Executing as a Unix TIL --- README.md | 3 +- ...script-shellcheck-itself-when-executing.md | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 unix/have-script-shellcheck-itself-when-executing.md diff --git a/README.md b/README.md index ae24784..6bf6dec 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1630 TILs and counting..._ +_1631 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1588,6 +1588,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Grep For Files Without A Match](unix/grep-for-files-without-a-match.md) - [Grep For Files With Multiple Matches](unix/grep-for-files-with-multiple-matches.md) - [Grep For Multiple Patterns](unix/grep-for-multiple-patterns.md) +- [Have Script ShellCheck Itself When Executing](unix/have-script-shellcheck-itself-when-executing.md) - [Hexdump A Compiled File](unix/hexdump-a-compiled-file.md) - [Ignore A Directory During ripgrep Search](unix/ignore-a-directory-during-ripgrep-search.md) - [Ignore The Alias When Running A Command](unix/ignore-the-alias-when-running-a-command.md) diff --git a/unix/have-script-shellcheck-itself-when-executing.md b/unix/have-script-shellcheck-itself-when-executing.md new file mode 100644 index 0000000..ae4d2cd --- /dev/null +++ b/unix/have-script-shellcheck-itself-when-executing.md @@ -0,0 +1,60 @@ +# Have Script ShellCheck Itself When Executing + +The [ShellCheck](https://www.shellcheck.net/) utility can be run against bash +scripts to check if there are any warnings or errors we should fix. It works +great as long as we remember to run it. + +I wondered if I could make it easier on myself by not having to remember to run +it. What if my bash script were to `shellcheck` itself? + +Here is an example script where at the beginning it looks for and runs the +`shellcheck` utility against `$0` (the path of the script). This is kind of +meta. As the script is executing, it has an external program run against the +entire contents of itself. If there are any `shellcheck` issues, they get +displayed and the program exits early. + +```bash +#!/bin/bash + +# Exit immediately if any command fails +set -e + +# Self-validation using ShellCheck +if command -v shellcheck &> /dev/null; then + echo "Validating script with ShellCheck..." + + # $0 refers to the script itself + if ! shellcheck "$0"; then + echo "ShellCheck found issues in the script. Exiting." + exit 1 + fi + echo "Script validation passed." +else + echo "Warning: ShellCheck not found. Skipping validation." +fi + +echo "Script execution continuing..." + +# shellcheck warning here +read -p "Continue with current operation? (yes/no): " CONTINUE_WITH_EXISTING +if [[ ! "$CONTINUE_WITH_EXISTING" =~ ^[Yy][Ee][Ss]$ ]]; then + echo "Operation cancelled." + exit 1 +fi +``` + +This last bit of the script with the `read` command will trigger a warning from +`shellcheck`. + +```bash +$ ./check.sh +Validating script with ShellCheck... + +In ./check.sh line 23: +read -p "Continue with current operation? (yes/no): " CONTINUE_WITH_EXISTING +^--^ SC2162 (info): read without -r will mangle backslashes. + +For more information: + https://www.shellcheck.net/wiki/SC2162 -- read without -r will mangle backs... +ShellCheck found issues in the script. Exiting. +```