lint-shell-scripts.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. set -eo pipefail
  3. script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
  4. cd "$script_path/.."
  5. if [ "$#" -eq "0" ]; then
  6. files=()
  7. while IFS= read -r file; do
  8. files+=("$file")
  9. done < <(
  10. git ls-files -- \
  11. '*.sh' \
  12. )
  13. else
  14. files=()
  15. for file in "$@"; do
  16. # Skip ports, like we in the CI case above.
  17. if [[ "${file}" =~ "Ports" ]]; then
  18. continue
  19. fi
  20. if [[ "${file}" == *".sh" && "${file}" != "Base/root/generate_manpages.sh" ]]; then
  21. files+=("${file}")
  22. fi
  23. done
  24. fi
  25. if (( ${#files[@]} )); then
  26. if ! command -v shellcheck &>/dev/null ; then
  27. echo "shellcheck is not available, but shell files need linting! Either skip this script, or install shellcheck."
  28. exit 1
  29. fi
  30. shellcheck --source-path=SCRIPTDIR "${files[@]}"
  31. for file in "${files[@]}"; do
  32. if (< "$file" grep -qE "grep [^|);]*-[^- ]*P"); then
  33. # '\x2D' is the unicode escape sequence for '-'. This is used so
  34. # that this script does not flag itself for containing grep dash P.
  35. echo -e "The script '$file' contains 'grep \x2DP', which is not supported on macOS. Please use grep -E instead."
  36. exit 1
  37. fi
  38. done
  39. else
  40. echo "No .sh files to check."
  41. fi