Added POSIX compatible options without shellcheck warnings

Vidar Holen
2019-03-17 19:00:55 -07:00
parent ba2d0b5128
commit b501ea60b5

@@ -10,36 +10,32 @@ Script could have a `.sh` extension, no extension and have a range of shebang li
The solution for this problem is to use `shellcheck` in combination with the `find` or `grep` command. The solution for this problem is to use `shellcheck` in combination with the `find` or `grep` command.
## Without exit code ## By extension
If you're not interested in the exit code of the `shellcheck` command, you can use (a variation) of one of the following commands: To check files with one of multiple extensions:
``` ```
# Scan a complete folder (recursively) # Bash 4+
find path/to/scripts -type f -exec "shellcheck" "--format=gcc" {} \; shopt -s globstar nullglob
shellcheck /path/to/scripts/**/*.{sh,bash,ksh}
# Scan a complete folder (recursively) for .sh files # POSIX
find path/to/scripts -type f -name "*.sh" -exec "shellcheck" "--format=gcc" {} \; find /path/to/scripts -type f \( -name "*.sh" -o -name "*.bash" -o -name "*.ksh" \) -print |
while IFS="" read -r file
do
shellcheck "$file"
done
``` ```
## With exit code ## By shebang
If you want to use `shellcheck` in some kind of testing pipeline, you want to know the exit code of the command. To check files whose shebang indicate that they are sh/bash/ksh scripts:
In this case you can test every matched file individually:
``` ```
# Scan a complete folder (recursively) # POSIX
for file in $(find path/to/scripts -type f); do shellcheck --format=gcc $file; done; find /path/to/scripts -type f -exec grep -Eq '^#!(.*/|.*env +)(sh|bash|ksh)' {} \; -print |
while IFS="" read -r file
# Scan a complete folder (recursively) for .sh files do
for file in $(find path/to/scripts -type f -name "*.sh"); do shellcheck --format=gcc $file; done; shellcheck "$file"
``` done
## Finding files to test
Since not all scripts have a `.sh` extension, you could use the shebang line of files to determine if they need to be tested (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;
``` ```