linux - 如何抑制不相关的 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."
在哪里:
am_i_root
正在检查传递的不需要的参数。它的真正目的是不言自明的。print_error_and_exit
就像它的名字所说的那样,它或多或少是不言自明的。如果传递了任何参数,我希望函数/脚本打印错误消息并退出。
问题
如何禁用这些消息(仅限本地)?
解决方案
在做这件事之前要考虑清楚!
仅当您 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
推荐阅读
- python - Tkinter 网格“粘性”不适用于网格传播
- python - 如何将 tf.random 用于形状为 None 的张量
- c++ - 多字符常量警告
- python - 如何读取数组并返回数组?
- ios - 带有 InsetGroupedListStyle 的 SwiftUI 列表仅在滚动后更新正确的行高
- c# - C# - 如何过滤条件 A 的列表,然后仅针对这些匹配,按条件 B 过滤,而不破坏列表顺序
- c# - 从给定的一组数字构造到给定长度的所有可能数字的计数
- reactjs - 如何使用 React Redux 使用 typicode json 服务器获取数据
- go - 找到接口的所有实现并将它们放在一个切片中
- python - eth_account Account.encrypt 和 Account.decrypt