系统:Linux Mint 19(基于Ubuntu 18.04)。
编辑器:我将Visual Studio Code(official website)与ShellCheck插件配合使用来即时检查错误,警告和提示。
是每个shell脚本编写者必备的工具。
尽管开发人员必须付出巨大的努力才能使其达到最佳状态,但有时会产生不相关的警告和/或信息。
示例代码,带有此类消息(警告SC2120 +直接相邻的信息SC2119):
am_i_root()
# expected arguments: none
{
# check if no argument has been passed
[ "${#}" -eq 0 ] || print_error_and_exit 1 "am_i_root" "Some arguments have been passed to the function!\\n\\tNo arguments expected.\\n\\tPassed: ${*}"
# check if the user is root
# this will return an exit code of the command itself directly
[ "$(id -u)" -eq 0 ]
}
# check if the user had by any chance run the script with root privileges and if so, quit
am_i_root && print_error_and_exit 1 "am_i_root" "This script should not be run as root!\\n\\tQuiting to safety."
位置:
am_i_root
正在检查是否传递了不需要的参数。它的真正目的是不言自明的。
print_error_and_exit
就像它的名字所说的那样,或多或少是不言自明的。
如果已传递任何参数,我希望函数/脚本显示错误消息并退出。
如何禁用这些消息(仅限本地)?
答案 0 :(得分:1)
使用 Shellcheck 0.7.1 及更高版本,您可以通过过滤严重性(有效选项为:错误、警告、信息、样式)来抑制命令行上不相关的消息:
$ shellcheck --severity=error my_script.sh
这只会显示错误,并会抑制烦人的 SC2034
、SC2086
等警告和样式建议。
您还可以使用 .shellcheckrc 文件中的指令抑制每个代码的消息,例如:
disable=SC2076,SC2016
这两个选项都允许您全局过滤消息,而不必使用相同的指令编辑每个源代码文件。
如果您的发行版没有最新版本,您可以upgrade:
scversion="stable" # or "0.7.1" or "latest"
wget -qO- "https://github.com/koalaman/shellcheck/releases/download/${scversion?}/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv
sudo cp "shellcheck-${scversion}/shellcheck" /usr/bin/
shellcheck --version
答案 1 :(得分:0)