mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Have Script ShellCheck Itself When Executing as a Unix TIL
This commit is contained in:
@@ -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).
|
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:
|
See some of the other learning resources I work on:
|
||||||
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
|
- [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 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 Files With Multiple Matches](unix/grep-for-files-with-multiple-matches.md)
|
||||||
- [Grep For Multiple Patterns](unix/grep-for-multiple-patterns.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)
|
- [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 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)
|
- [Ignore The Alias When Running A Command](unix/ignore-the-alias-when-running-a-command.md)
|
||||||
|
|||||||
60
unix/have-script-shellcheck-itself-when-executing.md
Normal file
60
unix/have-script-shellcheck-itself-when-executing.md
Normal file
@@ -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.
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user