Add a page about how to use shellcheck recursively

Jachim Coudenys
2018-02-23 09:05:49 +01:00
parent 2ea4bed1c1
commit 5aafb57021

45
Recursiveness.md Normal file

@@ -0,0 +1,45 @@
By default, `shellcheck` does not support a `-r` option. The reason for this, is that there are different matching patterns for different files.
Script could have a `.sh` extension, no extension and have a range of shebang lines (which have there own testing format):
* `#!/bin/bash`
* `#!/bin/sh`
* `#!/usr/bin/env bash`
* `#!/usr/bin/env sh`
* etc...
The solution for this problem is to use `shellcheck` in combination with the `find` or `grep` command.
## Without exit code
If you're not interested in the exit code of the `shellcheck` command, you can use (a variation) of one of the following commands:
```
# Scan a complete folder (recursively)
find path/to/scripts -type f -exec "shellcheck" "--format=gcc" {} \;
# Scan a complete folder (recursively) for .sh files
find path/to/scripts -type f -name "*.sh" -exec "shellcheck" "--format=gcc" {} \;
```
## With exit code
If you want to use `shellcheck` in some kind of testing pipeline, you want to know the exit code of the command.
In this case you can test every matched file individually:
```
# Scan a complete folder (recursively)
for file in $(find path/to/scripts -type f); do shellcheck --format=gcc $file; done;
# Scan a complete folder (recursively) for .sh files
for file in $(find path/to/scripts -type f -name "*.sh"); do shellcheck --format=gcc $file; done;
```
## Finding files to test
Since not all script have a `.sh` extension, you could use the shebang line of files to determine if they need to be testen (and with which format):
```
for file in $(grep -IRl "#\!\(/usr/bin/env \|/bin/\)sh" --exclude-dir "var" --exclude "*.txt"); do shellcheck --format=gcc --shell=sh $file; done;
```