代码之家  ›  专栏  ›  技术社区  ›  Vlastimil Burián

如何抑制不相关的ShellCheck消息?

  •  0
  • Vlastimil Burián  · 技术社区  · 6 年前

    环境

    系统:Linux Mint 19(基于ubuntu18.04)。

    Visual Studio Code ( official website )使用ShellCheck插件可以动态检查错误、警告和提示。


    贝壳花纹

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

    示例代码,带有这样的消息(警告 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 正如它的名字所说,它或多或少是不言自明的。

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


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

    1 回复  |  直到 6 年前
        1
  •  6
  •   Vlastimil Burián    6 年前

    在做这件事之前好好想想!

    只有当你100.0%肯定信息确实不相关时才这样做。然后,阅读维基 here here 关于这个话题。


    一旦你确信这些信息是无关紧要的

    虽然一般来说,有更多的方法来实现这个目标,我说禁用这些消息

    在实际消息发生之前添加以下行:

    # shellcheck disable=code
    
        2
  •  1
  •   enharmonic    4 年前

    $ shellcheck --severity=error my_script.sh
    

    SC2034 , SC2086 警告和样式建议。

    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