环境
系统: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
就像它的名字所说的那样,它或多或少是不言自明的。如果传递了任何参数,我希望函数/脚本打印错误消息并退出。
问题
如何禁用这些消息(仅限本地)?