針對 shell script 測試工具(shellcheck)

  • 因為最近用 AI 協助撰寫不少 shell script , 愈寫愈複雜, 想想應該要找個測試工具來幫忙
  • 以下是使用 shellcheck 的方式
  • Ubuntu / Debian

    sudo apt install shellcheck

  • 只修正 critical 項目

    shellcheck -S error scripts/*.sh

  • 範例:
    • 全部列出

      jonathan@200816-NB:~/github/docker-compose$ shellcheck scripts/*.sh
      
      In scripts/alpine_upgrade.sh line 23:
          echo -e "${BLUE}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
               ^-- SC3037 (warning): In POSIX sh, echo flags are undefined.
      
      
      In scripts/alpine_upgrade.sh line 27:
          echo -e "${GREEN}[SUCCESS]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
               ^-- SC3037 (warning): In POSIX sh, echo flags are undefined.
      
      
      In scripts/alpine_upgrade.sh line 31:
          echo -e "${YELLOW}[WARNING]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
               ^-- SC3037 (warning): In POSIX sh, echo flags are undefined.
      
      
      In scripts/alpine_upgrade.sh line 35:
          echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
               ^-- SC3037 (warning): In POSIX sh, echo flags are undefined.
      
      
      In scripts/alpine_upgrade.sh line 51:
      trap cleanup_on_error ERR
                            ^-^ SC3047 (warning): In POSIX sh, trapping ERR is undefined.
      
      :

    • 只看 error

      jonathan@200816-NB:~/github/docker-compose$ shellcheck -S error scripts/*.sh
      jonathan@200816-NB:~/github/docker-compose$

  • tech/shellcheck.txt
  • 上一次變更: 2025/10/07 11:59
  • jonathan