shellcheck: enable shell linting

Add to the `format-code.sh` optional support for linting shell scripts
using `shellcheck`.  All repositories will have this ran, but only
repositories with a `.shellcheck` file in the root of the repository
will stop on error.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ie25c27cffc96b7fa66291abef1b0f90a0f1862ed
diff --git a/build-unit-test-docker.sh b/build-unit-test-docker.sh
index d10857f..bea8030 100755
--- a/build-unit-test-docker.sh
+++ b/build-unit-test-docker.sh
@@ -239,6 +239,7 @@
     clang-format-10 \
     clang-tidy-10 \
     clang-tools-10 \
+    shellcheck \
     npm \
     iproute2 \
     libnl-3-dev \
diff --git a/scripts/format-code.sh b/scripts/format-code.sh
index dc378f8..1bb98a5 100755
--- a/scripts/format-code.sh
+++ b/scripts/format-code.sh
@@ -24,6 +24,20 @@
   fi
 fi
 
+# If .shellcheck exists, stop on error.  Otherwise, allow pass.
+if [[ -f ".shellcheck" ]]; then
+  shellcheck_allowfail="false"
+else
+  shellcheck_allowfail="true"
+fi
+
+# Run shellcheck on any shell-script.
+shell_scripts="$(git ls-files | xargs -n1 file -0 | \
+                 grep -a "shell script" | cut -d '' -f 1)"
+for script in ${shell_scripts}; do
+  shellcheck -x "${script}" || ${shellcheck_allowfail}
+done
+
 # Allow called scripts to know which clang format we are using
 export CLANG_FORMAT="clang-format-10"
 IGNORE_FILE=".clang-ignore"