Skip to content

Instantly share code, notes, and snippets.

@flying-sausages
Last active May 29, 2021 12:18
Show Gist options
  • Save flying-sausages/5c5363ff85bc69378553bbeaffc3f0d2 to your computer and use it in GitHub Desktop.
Save flying-sausages/5c5363ff85bc69378553bbeaffc3f0d2 to your computer and use it in GitHub Desktop.
Automatically apply shellcheck diff recursively

Automatically apply shellcheck diff recursively

That's right!

Why?

  1. Shellcheck can generate a diff of the changes it would auto-fix, but there's no flag to just apply them
  2. patch can be a bit annoying recursively
  3. A normal bash forloop over files only of specific names is a bit annoying

And this does?

Finds all files that end with .sh inside of the current directory, generates the diff for it, and immediately patches it

#!/bin/bash
#To apply _all_ possible changes
find . -type f -name \*.sh -exec bash -c 'shellcheck "$1" --format=diff | patch "$1"' none {} \;
#Using an SC-code "whitelist"
export EXPLICIT_SHCK_CODES="SC2086" #Fix quotes
find . -type f -name \*.sh -exec bash -c 'shellcheck "$1" -i $EXPLICIT_SHCK_CODES --format=diff | patch "$1"' none {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment