From 38db21a2b694474c1a1165372d81110934e6c5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 19:21:56 +0200 Subject: [PATCH 01/19] Add GitHub Actions workflow for ShellCheck --- .github/workflows/test.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000000..08e617b9ef5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,10 @@ +name: Test + +on: [push, pull_request] + +jobs: + shellcheck: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2.3.4 + - uses: bewuethr/shellcheck-action@v2 From 227873fe043ce78e90628c39f9675b23e528ba31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 19:22:08 +0200 Subject: [PATCH 02/19] Add Dependabot configuration for GitHub Actions --- .github/dependabot.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000000..8b029af6173 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,9 @@ +version: 2 +updates: +- package-ecosystem: github-actions + directory: "/" + schedule: + interval: daily + time: "03:00" + timezone: Europe/Oslo + open-pull-requests-limit: 99 From 4287fe1a5304a3eb0e74690a259705ea74975fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 19:28:34 +0200 Subject: [PATCH 03/19] Fix SC2197 Fix SC2197: fgrep is non-standard and deprecated. Use grep -F instead. https://www.shellcheck.net/wiki/SC2197 --- brew.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brew.sh b/brew.sh index 26508ee43dd..0a69ef615e8 100755 --- a/brew.sh +++ b/brew.sh @@ -27,7 +27,7 @@ brew install bash brew install bash-completion2 # Switch to using brew-installed bash as default shell -if ! fgrep -q "${BREW_PREFIX}/bin/bash" /etc/shells; then +if ! grep -Fq "${BREW_PREFIX}/bin/bash" /etc/shells; then echo "${BREW_PREFIX}/bin/bash" | sudo tee -a /etc/shells; chsh -s "${BREW_PREFIX}/bin/bash"; fi; From ca8f9e44a85f05f0ffa7c340ceeec157b17246ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 23:31:59 +0200 Subject: [PATCH 04/19] Fix SC2164 Fix SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. https://www.shellcheck.net/wiki/SC2164 --- .functions | 4 ++-- bootstrap.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.functions b/.functions index bce0305db69..901ece8099f 100644 --- a/.functions +++ b/.functions @@ -2,12 +2,12 @@ # Create a new directory and enter it function mkd() { - mkdir -p "$@" && cd "$_"; + mkdir -p "$@" && cd "$_" || return; } # Change working directory to the top-most Finder window location function cdf() { # short for `cdfinder` - cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"; + cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')" || return; } # Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression diff --git a/bootstrap.sh b/bootstrap.sh index 5894c6c22f2..ee0aff98ca6 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -cd "$(dirname "${BASH_SOURCE}")"; +cd "$(dirname "${BASH_SOURCE}")" || exit; git pull origin main; From 9696540a58c89b41ad8cdda9865da74be52829c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 23:37:05 +0200 Subject: [PATCH 05/19] Fix SC2128 Fix SC2128: Expanding an array without an index only gives the first element. https://www.shellcheck.net/wiki/SC2128 --- bootstrap.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap.sh b/bootstrap.sh index ee0aff98ca6..1246a27fc46 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -cd "$(dirname "${BASH_SOURCE}")" || exit; +cd "$(dirname "${BASH_SOURCE[0]:-$0}")" || exit; git pull origin main; From a7c246fca058d2e1eb1284439cb65b17266861d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 23:52:17 +0200 Subject: [PATCH 06/19] Ignore SC1090 Ignore SC1090: ShellCheck can't follow non-constant source. Use a directive to specify location. https://www.shellcheck.net/wiki/SC1090 See the following discussion for why SC1090 is disabled and not fixed: https://github.com/koalaman/shellcheck/issues/507 --- bootstrap.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bootstrap.sh b/bootstrap.sh index 1246a27fc46..2bc1bb1f182 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -12,6 +12,7 @@ function doIt() { --exclude "README.md" \ --exclude "LICENSE-MIT.txt" \ -avh --no-perms . ~; + # shellcheck disable=SC1090 source ~/.bash_profile; } From 29986785f7ebac60a0e0c4ffb5c248818cd9b109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Thu, 8 Jul 2021 23:56:33 +0200 Subject: [PATCH 07/19] Fix SC2166 Fix SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined. https://www.shellcheck.net/wiki/SC2166 --- bootstrap.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap.sh b/bootstrap.sh index 2bc1bb1f182..81d11f167d8 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -16,7 +16,7 @@ function doIt() { source ~/.bash_profile; } -if [ "$1" == "--force" -o "$1" == "-f" ]; then +if [ "$1" == "--force" ] || [ "$1" == "-f" ]; then doIt; else read -p "This may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1; From b9d0c677423be421ff49d2c8965dcaa9bc9f8777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:01:20 +0200 Subject: [PATCH 08/19] Fix SC2162 Fix SC2162: read without -r will mangle backslashes. https://www.shellcheck.net/wiki/SC2162 --- bootstrap.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap.sh b/bootstrap.sh index 81d11f167d8..117c95b28da 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -19,7 +19,7 @@ function doIt() { if [ "$1" == "--force" ] || [ "$1" == "-f" ]; then doIt; else - read -p "This may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1; + read -rp "This may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1; echo ""; if [[ $REPLY =~ ^[Yy]$ ]]; then doIt; From fa68f55365d34419bad511181c6ff12bf9bbe09d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:03:57 +0200 Subject: [PATCH 09/19] Fix SC2091 Fix SC2091: Remove surrounding $() to avoid executing output (or use eval if intentional). https://www.shellcheck.net/wiki/SC2091 --- .bash_prompt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.bash_prompt b/.bash_prompt index 74db549f994..78b2c45ca34 100644 --- a/.bash_prompt +++ b/.bash_prompt @@ -35,11 +35,11 @@ prompt_git() { s+='*'; else # Check for uncommitted changes in the index. - if ! $(git diff --quiet --ignore-submodules --cached); then + if ! git diff --quiet --ignore-submodules --cached; then s+='+'; fi; # Check for unstaged changes. - if ! $(git diff-files --quiet --ignore-submodules --); then + if ! git diff-files --quiet --ignore-submodules --; then s+='!'; fi; # Check for untracked files. @@ -47,7 +47,7 @@ prompt_git() { s+='?'; fi; # Check for stashed files. - if $(git rev-parse --verify refs/stash &>/dev/null); then + if git rev-parse --verify refs/stash &>/dev/null; then s+='$'; fi; fi; From b0c800fa3a678b034471d988ec6d74e993fb8f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:12:59 +0200 Subject: [PATCH 10/19] Ignore SC2034 Ignore SC2034: foo appears unused. Verify use (or export if used externally). https://www.shellcheck.net/wiki/SC2034 --- .bash_prompt | 1 + 1 file changed, 1 insertion(+) diff --git a/.bash_prompt b/.bash_prompt index 78b2c45ca34..76a7b85ac58 100644 --- a/.bash_prompt +++ b/.bash_prompt @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2034 # Shell prompt based on the Solarized Dark theme. # Screenshot: http://i.imgur.com/EkEtphC.png From 629eab34fc9cf824b13d2d881eec8fecc9aaa4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:14:40 +0200 Subject: [PATCH 11/19] Ignore SC2154 Ignore SC2154: yellow is referenced but not assigned. https://www.shellcheck.net/wiki/SC2154 --- .exports | 1 + 1 file changed, 1 insertion(+) diff --git a/.exports b/.exports index 02cd3cdae8d..e5ca05aa957 100644 --- a/.exports +++ b/.exports @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2154 # Make vim the default editor. export EDITOR='vim'; From e18d3b844ec98edd44e8a3cf2849118f296ff6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:23:50 +0200 Subject: [PATCH 12/19] Fix SC2155 Fix SC2155: Declare and assign separately to avoid masking return values. https://www.shellcheck.net/wiki/SC2155 --- .exports | 3 ++- .functions | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.exports b/.exports index e5ca05aa957..0ff3f4c007c 100644 --- a/.exports +++ b/.exports @@ -32,7 +32,8 @@ export MANPAGER='less -X'; # Avoid issues with `gpg` as installed via Homebrew. # https://stackoverflow.com/a/42265848/96656 -export GPG_TTY=$(tty); +GPG_TTY=$(tty); +export GPG_TTY # Hide the “default interactive shell is now zsh” warning on macOS. export BASH_SILENCE_DEPRECATION_WARNING=1; diff --git a/.functions b/.functions index 901ece8099f..d53b05b015c 100644 --- a/.functions +++ b/.functions @@ -68,7 +68,8 @@ fi; # Create a data URL from a file function dataurl() { - local mimeType=$(file -b --mime-type "$1"); + local mimeType; + mimeType=$(file -b --mime-type "$1"); if [[ $mimeType == text/* ]]; then mimeType="${mimeType};charset=utf-8"; fi @@ -87,17 +88,19 @@ function server() { # Start a PHP server from a directory, optionally specifying the port # (Requires PHP 5.4.0+.) function phpserver() { - local port="${1:-4000}"; - local ip=$(ipconfig getifaddr en1); + local port, ip; + port="${1:-4000}"; + ip=$(ipconfig getifaddr en1); sleep 1 && open "http://${ip}:${port}/" & php -S "${ip}:${port}"; } # Compare original and gzipped file size function gz() { - local origsize=$(wc -c < "$1"); - local gzipsize=$(gzip -c "$1" | wc -c); - local ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l); + local origsize, gzipsize, ratio; + origsize=$(wc -c < "$1"); + gzipsize=$(gzip -c "$1" | wc -c); + ratio=$(echo "$gzipsize * 100 / $origsize" | bc -l); printf "orig: %d bytes\n" "$origsize"; printf "gzip: %d bytes (%2.2f%%)\n" "$gzipsize" "$ratio"; } @@ -115,15 +118,17 @@ function getcertnames() { return 1; fi; - local domain="${1}"; + local domain, tmp, certText; + + domain="${1}"; echo "Testing ${domain}…"; echo ""; # newline - local tmp=$(echo -e "GET / HTTP/1.0\nEOT" \ + tmp=$(echo -e "GET / HTTP/1.0\nEOT" \ | openssl s_client -connect "${domain}:443" -servername "${domain}" 2>&1); if [[ "${tmp}" = *"-----BEGIN CERTIFICATE-----"* ]]; then - local certText=$(echo "${tmp}" \ + certText=$(echo "${tmp}" \ | openssl x509 -text -certopt "no_aux, no_header, no_issuer, no_pubkey, \ no_serial, no_sigdump, no_signame, no_validity, no_version"); echo "Common Name:"; From 7db6203ae08b4cac2e5206f16931069382688d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:25:09 +0200 Subject: [PATCH 13/19] Fix SC1018 Fix SC1018: This is a unicode non-breaking space. Delete and retype it. https://www.shellcheck.net/wiki/SC1018 --- .functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.functions b/.functions index d53b05b015c..47b85156afa 100644 --- a/.functions +++ b/.functions @@ -2,7 +2,7 @@ # Create a new directory and enter it function mkd() { - mkdir -p "$@" && cd "$_" || return; + mkdir -p "$@" && cd "$_" || return; } # Change working directory to the top-most Finder window location From e187a8f6010049f165223ba1d4164034fa194960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:30:30 +0200 Subject: [PATCH 14/19] Fix SC2124 Fix SC2124: Assigning an array to a string! Assign as array, or use * instead of @ to concatenate. https://www.shellcheck.net/wiki/SC2124 --- .functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.functions b/.functions index 47b85156afa..31499c4a206 100644 --- a/.functions +++ b/.functions @@ -12,7 +12,7 @@ function cdf() { # short for `cdfinder` # Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression function targz() { - local tmpFile="${@%/}.tar"; + local tmpFile="${*%/}.tar"; tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1; size=$( From 6eed23f77853aa05d081c940f88295142923b256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:31:32 +0200 Subject: [PATCH 15/19] Fix SC2199 Fix SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @). https://www.shellcheck.net/wiki/SC2199 --- .functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.functions b/.functions index 31499c4a206..6875a125e77 100644 --- a/.functions +++ b/.functions @@ -51,7 +51,7 @@ function fs() { else local arg=-sh; fi - if [[ -n "$@" ]]; then + if [[ -n "$*" ]]; then du $arg -- "$@"; else du $arg .[^.]* ./*; From 8926ad3313e0105f7317da5fe811c33e723838e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:35:02 +0200 Subject: [PATCH 16/19] Fix SC2181 Fix SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. https://www.shellcheck.net/wiki/SC2181 --- .functions | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.functions b/.functions index 6875a125e77..ac6a1096efb 100644 --- a/.functions +++ b/.functions @@ -59,8 +59,7 @@ function fs() { } # Use Git’s colored diff when available -hash git &>/dev/null; -if [ $? -eq 0 ]; then +if hash git &>/dev/null; then function diff() { git diff --no-index --color-words "$@"; } From 3a9318d9b0f6b0632cd3ba3bf358ae552c1a04fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:36:21 +0200 Subject: [PATCH 17/19] Fix SC2046 Fix SC2046: Quote this to prevent word splitting. https://www.shellcheck.net/wiki/SC2046 --- .functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.functions b/.functions index ac6a1096efb..706bda4a62a 100644 --- a/.functions +++ b/.functions @@ -147,7 +147,7 @@ function getcertnames() { # Normalize `open` across Linux, macOS, and Windows. # This is needed to make the `o` function (see below) cross-platform. -if [ ! $(uname -s) = 'Darwin' ]; then +if [ ! "$(uname -s)" = 'Darwin' ]; then if grep -q Microsoft /proc/version; then # Ubuntu on Windows using the Linux subsystem alias open='explorer.exe'; From c3d07f2ce4e935b21e51b8e7fafdae300cbf7391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:39:48 +0200 Subject: [PATCH 18/19] Ignore SC2139 Ignore SC2139: This expands when defined, not when used. Consider escaping. https://www.shellcheck.net/wiki/SC2139 --- .aliases | 1 + 1 file changed, 1 insertion(+) diff --git a/.aliases b/.aliases index 3387fa0609e..7d286e82346 100644 --- a/.aliases +++ b/.aliases @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck disable=SC2139 # Easier navigation: .., ..., ...., ....., ~ and - alias ..="cd .." From be97206940fb3e34461568a95ab54fa68de107de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Fri, 9 Jul 2021 00:44:13 +0200 Subject: [PATCH 19/19] Fix SC2140 Fix SC2140: Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"? https://www.shellcheck.net/wiki/SC2140 --- .aliases | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.aliases b/.aliases index 7d286e82346..fe45c90def0 100644 --- a/.aliases +++ b/.aliases @@ -128,7 +128,7 @@ alias map="xargs -n1" # One of @janmoesen’s ProTip™s for method in GET HEAD POST PUT DELETE TRACE OPTIONS; do - alias "${method}"="lwp-request -m '${method}'" + alias "${method}=lwp-request -m '${method}'" done # Stuff I never really use but cannot delete either because of http://xkcd.com/530/