首页 > 解决方案 > 如何抑制不相关的 ShellCheck 消息?

问题描述

环境

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

编辑:我使用带有 ShellCheck 插件的Visual Studio Code官方网站)来即时检查错误、警告和提示。


壳牌检查

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

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

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


示例 shell 脚本片段

am_i_root ()
# expected arguments: none
{
    # check if no argument has been passed
    [ "$#" -eq 0 ] || print_error_and_exit "am_i_root" "Some arguments have been passed to the function! No arguments expected. Passed: $*"

    # 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 "am_i_root" "This script should not be run as root! Quitting to shell."

在哪里:


问题

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

标签: linuxshellshellcheck

解决方案


在做这件事之前要考虑清楚!

仅当您 100.0% 肯定消息确实无关紧要时才这样做。然后,在此处此处阅读有关此主题的 Wiki 。


一旦您向自己保证,该消息是无关紧要的

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

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

# shellcheck disable=code

值得注意的是,在同一行之后添加文本将导致错误,因为它也会被 shellcheck 解释。如果要添加关于为什么要禁止警告的解释,可以添加另一个哈希#以防止 shellcheck 解释该行的其余部分。

不正确:

# shellcheck disable=code irrelevant because reasons

正确的:

# shellcheck disable=code # code is irrelevant because reasons

请注意,可以添加多个用逗号分隔的代码,如下例所示:

# shellcheck disable=SC2119,SC2120

推荐阅读