shellcheck.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/sh
  2. # Note use sh and not bash!
  3. # To be used with in shellcheck and automated usage
  4. # Generate timestamp
  5. # If logging specified, export shellcheck output to log
  6. # Excluded codes
  7. # SC2086 = SC2086: Double quote to prevent globbing and word splitting. - Keep it more readable please use them with v-xxx-commands when used user input might be not validated corrections and whitespaces might cause a risk
  8. # SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
  9. # Check exit code directly with e.g. 'if mycmd;', not indirectly with $?. Might be worth disable in in the future
  10. # SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
  11. # SC2153: Possible misspelling: DOMAIN may not be assigned, but domain is. - Issues with SOURCE importing vars that are not defined in the script it self but config files
  12. # SC2016: Expressions don't expand in single quotes, use double quotes for that. - History reasons
  13. # SC2196: egrep is non-standard and deprecated. Use grep -E instead. Todo be removed in the future
  14. # SC1090; Can't follow non-constant source. Use a directive to specify location. - Hestia loves $HESTIA/data/ips/$ip
  15. # SC2031: var was modified in a subshell. That change might be lost.
  16. # SC2010
  17. # SC2143
  18. # SC2046
  19. #set default value for error
  20. err=0
  21. shellcheck --version
  22. i=0
  23. f=0
  24. files=$(grep -rlE '#!/bin/(bash|sh)' ./ | grep -vE '\.(git|j2$|md$)')
  25. for file in $files; do
  26. i=$(($i + 1))
  27. shellcheck -x "$file" --severity="error"
  28. # Only show failed checks
  29. if [ $? -gt 0 ]; then
  30. f=$(($f + 1))
  31. echo "Linting: $file"
  32. printf "%s: \033[0;31m Fail \033[0m\n" "$file"
  33. err=1
  34. fi
  35. done
  36. echo "$i files checked and $f errors"
  37. exit $err