summaryrefslogtreecommitdiff
path: root/src/content/tils/2020/12/15/shellcheck-repo.adoc
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/tils/2020/12/15/shellcheck-repo.adoc')
-rw-r--r--src/content/tils/2020/12/15/shellcheck-repo.adoc17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/content/tils/2020/12/15/shellcheck-repo.adoc b/src/content/tils/2020/12/15/shellcheck-repo.adoc
index db280db..387e793 100644
--- a/src/content/tils/2020/12/15/shellcheck-repo.adoc
+++ b/src/content/tils/2020/12/15/shellcheck-repo.adoc
@@ -12,7 +12,7 @@ In my repositories I usually have Bash and POSIX scripts, which I want to keep
tidy with {shellcheck}[ShellCheck]. Here's the first version of
`assert-shellcheck.sh`:
-[source,shell]
+[source,sh]
----
#!/bin/sh -eux
@@ -28,7 +28,7 @@ recently added some scripts without any extension, so `assert-shellcheck.sh`
called for a second version. The first attempt was to try grepping the shebang
line:
-[source,shell]
+[source,sh]
----
$ grep '^#!/' assert-shellcheck.sh
#!/usr/sh
@@ -37,7 +37,7 @@ $ grep '^#!/' assert-shellcheck.sh
Good, we have a grep pattern on the first try. Let's try to find all the
matching files:
-[source,shell]
+[source,sh]
----
$ find . -type f | xargs grep -l '^#!/'
./TODOs.org
@@ -76,7 +76,7 @@ sample hooks in `.git/hooks/*`.
To list the files that Git is tracking we'll try `git ls-files`:
-[source,shell]
+[source,sh]
----
$ git ls-files | xargs grep -l '^#!/'
TODOs.org
@@ -109,7 +109,7 @@ and to stop processing the file if the pattern matches. A quick search points
me to using `FNR` for the former, and `{ nextline }` for the latter. Let's try
it:
-[source,shell]
+[source,sh]
----
$ git ls-files | xargs awk 'FNR>1 { nextfile } /^#!\// { print FILENAME; nextfile }'
bootstrap
@@ -131,13 +131,14 @@ matching against any part of the file that may have a shebang-like line, we only
look for the first. Let's put it back into the `assert-shellcheck.sh` file and
use `NULL` for separators to accommodate files with spaces in the name:
-....
+[source,sh]
+----
#!/usr/sh -eux
git ls-files -z | \
xargs -0 awk 'FNR>1 { nextfile } /^#!\// { print FILENAME; nextfile }' | \
xargs shellcheck
-....
+----
This is where I've stopped, but I imagine a likely improvement: match against
only +#!/bin/sh+ and +#!/usr/bin/env bash+ shebangs (the ones I use most), to
@@ -152,7 +153,7 @@ on the POSIX subset of Awk for working correctly.
After publishing, I could remove `{ nextfile }` and even make the script
simpler:
-[source,shell]
+[source,sh]
----
#!/usr/sh -eux