Shellsheck 是一个shell脚本的静态分析工具,可以在一些Linux系统上本地安装,也可以在不在线安装的情况下使用,用于检查bash脚本的一些错误。
- https://github.com/koalaman/shellcheck
- https://www.shellcheck.net/
- https://man.archlinux.org/man/shellcheck.1.en
测试环境:
- Linux Mint(Ubuntu 版)
- 给出的是一个工作 bash 脚本,它回显“我是来自主文件的回显”和一个回显“我是来自源文件的回显”的源文件。
- 展位文件位于同一文件夹中。
- 通过本地安装版本使用 shellcheck 0.7.1-1 进行测试。
主文件
#!/bin/bash
source ./sourcefile.sh
echo "Output from main.sh"
echo
echo
fkt_output_from_sourcefile_sh
echo
echo
echo "For closing window, press Enter or Ctl + C"; read -r
源文件.sh
#!/bin/bash
fkt_output_from_sourcefile_sh() {
echo "Output from sourcefile.sh"
}
我如何在终端上运行它:
shellcheck -x main.sh
终端上的输出(看起来工作正常):
Output from main.sh
Output from sourcefile.sh
For closing window, press Enter or Ctl + C
通过 shellcheck -x 检查的错误消息:
In /home/user/desktop/main.sh line 8:
source ./sourcefile.sh
^-- SC1091: Not following: ./sourcefile.sh: openBinaryFile: does not exist (No such file or directory)
可能的解决方案(这对我不起作用,可能取决于我的错误语法):
- https://github.com/koalaman/shellcheck/wiki/SC1090
- https://github.com/koalaman/shellcheck/wiki/SC1091
- https://github.com/koalaman/shellcheck/issues/769
- https://github.com/koalaman/shellcheck/wiki/Directive
无效解决方案示例:
基于:“告诉 ShellCheck 在哪里可以找到源文件(从 0.4.0 开始):”
# shellcheck source=src/examples/config.sh
. "$(locate_config)"
资源:
主文件
#!/bin/bash
# shellcheck source=./sourcefile.sh
source "$(find_install_dir)/sourcefile.sh"
echo "Output from main.sh"
echo
echo
fkt_output_from_sourcefile_sh
echo
echo
echo "For closing window, press Enter or Ctl + C"; read -r
源文件.sh:
#!/bin/bash
fkt_output_from_sourcefile_sh() {
echo "Output from sourcefile.sh"
}
终端上的错误信息:
/home/user/desktop/main.sh: Line 4: find_install_dir: Command not found.
/home/user/desktop/main.sh: Line 4: /sourcefile.sh: File or folder not found
Output from main.sh
/home/user/desktop/main.sh: Line 10: fkt_output_from_sourcefile_sh: Command not found.
For closing window, press Enter or Ctl + C