DEV Community

Discussion on: Using `shellcheck` to lint your bash/sh scripts.

Collapse
 
biros profile image
Boris Jamot ✊ /

Hi @david_j_eddy , I have no access to my git repo at work now, but the idea is really simple. I use the following command to get all the staged files in my git index:

STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.sh`

Then, I loop on these files and run shellcheck against each of them:

for FILE in $STAGED_FILES_CMD
do
    shellcheck $FILE
    if [ $? != 0 ]
    then
        echo "Fix the errors before commit."
        exit 1
    fi
done

The idea being to break the commit in case of any error raised by shellcheck and let the developer correct before committing again.

It's nothing really extraordinary, but tools like this make your dev life easier every day.

For those who want to check everything at pre-commit stage, have a look at swagger-cli for your swagger spec, dockerlint for your Dockerfiles and composer validate for your composer.json.

Thread Thread
 
kip13 profile image
kip

Why did you use grep \\\\.sh 4 backslashes ?

Could be grep .sh$, no ?

Thread Thread
 
biros profile image
Boris Jamot ✊ /

I don't remember why, but I know I have to.
Maybe there's a simpler way.