Skip to content

Commit f97634a

Browse files
committed
Add Have Script ShellCheck Itself When Executing as a Unix TIL
1 parent 34ba60d commit f97634a

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1630 TILs and counting..._
13+
_1631 TILs and counting..._
1414

1515
See some of the other learning resources I work on:
1616
- [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
15881588
- [Grep For Files Without A Match](unix/grep-for-files-without-a-match.md)
15891589
- [Grep For Files With Multiple Matches](unix/grep-for-files-with-multiple-matches.md)
15901590
- [Grep For Multiple Patterns](unix/grep-for-multiple-patterns.md)
1591+
- [Have Script ShellCheck Itself When Executing](unix/have-script-shellcheck-itself-when-executing.md)
15911592
- [Hexdump A Compiled File](unix/hexdump-a-compiled-file.md)
15921593
- [Ignore A Directory During ripgrep Search](unix/ignore-a-directory-during-ripgrep-search.md)
15931594
- [Ignore The Alias When Running A Command](unix/ignore-the-alias-when-running-a-command.md)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Have Script ShellCheck Itself When Executing
2+
3+
The [ShellCheck](https://www.shellcheck.net/) utility can be run against bash
4+
scripts to check if there are any warnings or errors we should fix. It works
5+
great as long as we remember to run it.
6+
7+
I wondered if I could make it easier on myself by not having to remember to run
8+
it. What if my bash script were to `shellcheck` itself?
9+
10+
Here is an example script where at the beginning it looks for and runs the
11+
`shellcheck` utility against `$0` (the path of the script). This is kind of
12+
meta. As the script is executing, it has an external program run against the
13+
entire contents of itself. If there are any `shellcheck` issues, they get
14+
displayed and the program exits early.
15+
16+
```bash
17+
#!/bin/bash
18+
19+
# Exit immediately if any command fails
20+
set -e
21+
22+
# Self-validation using ShellCheck
23+
if command -v shellcheck &> /dev/null; then
24+
echo "Validating script with ShellCheck..."
25+
26+
# $0 refers to the script itself
27+
if ! shellcheck "$0"; then
28+
echo "ShellCheck found issues in the script. Exiting."
29+
exit 1
30+
fi
31+
echo "Script validation passed."
32+
else
33+
echo "Warning: ShellCheck not found. Skipping validation."
34+
fi
35+
36+
echo "Script execution continuing..."
37+
38+
# shellcheck warning here
39+
read -p "Continue with current operation? (yes/no): " CONTINUE_WITH_EXISTING
40+
if [[ ! "$CONTINUE_WITH_EXISTING" =~ ^[Yy][Ee][Ss]$ ]]; then
41+
echo "Operation cancelled."
42+
exit 1
43+
fi
44+
```
45+
46+
This last bit of the script with the `read` command will trigger a warning from
47+
`shellcheck`.
48+
49+
```bash
50+
$ ./check.sh
51+
Validating script with ShellCheck...
52+
53+
In ./check.sh line 23:
54+
read -p "Continue with current operation? (yes/no): " CONTINUE_WITH_EXISTING
55+
^--^ SC2162 (info): read without -r will mangle backslashes.
56+
57+
For more information:
58+
https://www.shellcheck.net/wiki/SC2162 -- read without -r will mangle backs...
59+
ShellCheck found issues in the script. Exiting.
60+
```

0 commit comments

Comments
 (0)