如何隐藏不相关的ShellCheck消息?

时间:2018-10-05 05:46:47

标签: linux shell shellcheck

环境

系统:Linux Mint 19(基于Ubuntu 18.04)。

编辑器:我将Visual Studio Codeofficial website)与ShellCheck插件配合使用来即时检查错误,警告和提示。


ShellCheck

是每个shell脚本编写者必备的工具。

尽管开发人员必须付出巨大的努力才能使其达到最佳状态,但有时会产生不相关的警告和/或信息。

示例代码,带有此类消息(警告SC2120 +直接相邻的信息SC2119):


POSIX shell脚本代码段

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就像它的名字所说的那样,或多或少是不言自明的。

  • 如果已传递任何参数,我希望函数/脚本显示错误消息并退出。


问题

如何禁用这些消息(仅限本地)?

2 个答案:

答案 0 :(得分:1)

使用 Shellcheck 0.7.1 及更高版本,您可以通过过滤严重性(有效选项为:错误、警告、信息、样式)来抑制命令行上不相关的消息:

$ shellcheck --severity=error my_script.sh

这只会显示错误,并会抑制烦人的 SC2034SC2086 等警告和样式建议。

您还可以使用 .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)

请仔细考虑!

仅当您肯定该消息确实无关紧要时,才执行100.0%的操作。然后,阅读有关该主题的Wiki herehere


一旦您向自己保证消息无关紧要

一般来说,有更多方法可以实现此目标,我说是本地禁用这些消息,所以实际上只有一个。

正在实际消息出现之前添加以下行:

# shellcheck disable=code