Bash 脚本命令和摆脱 shellcheck 错误 SC2181
Bash script command and getting rid of shellcheck error SC2181
我有以下 bash 脚本:
dpkg-query --show --showformat='${Status}\n' "$i" 2> \
/dev/null | grep "install ok installed" &> /dev/null
if [[ $? -eq 0 ]]; then
l_var_is_desktop="true"
fi
和 ShellCheck 实用程序 (https://www.shellcheck.net/) 给我以下输出:
$ shellcheck myscript
Line 17:
if [[ $? -eq 0 ]]; then
^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
$
此警告的 link 如下:https://github.com/koalaman/shellcheck/wiki/SC2181
修改它的最佳方法是什么。该命令真的太长了,无法放在一行中。我想避免使用 ShellCheck 忽略规则。
我试过创建局部变量并存储命令的输出,但这违反了其他规则。
命令直接放在 if
中并不会真正变得更长,您只是添加了 3 个字符。
if dpkg-query --show --showformat='${Status}\n' "$i" 2> \
/dev/null | grep "install ok installed" &> /dev/null
then
l_var_is_desktop="true"
fi
我有以下 bash 脚本:
dpkg-query --show --showformat='${Status}\n' "$i" 2> \
/dev/null | grep "install ok installed" &> /dev/null
if [[ $? -eq 0 ]]; then
l_var_is_desktop="true"
fi
和 ShellCheck 实用程序 (https://www.shellcheck.net/) 给我以下输出:
$ shellcheck myscript
Line 17:
if [[ $? -eq 0 ]]; then
^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.
$
此警告的 link 如下:https://github.com/koalaman/shellcheck/wiki/SC2181
修改它的最佳方法是什么。该命令真的太长了,无法放在一行中。我想避免使用 ShellCheck 忽略规则。
我试过创建局部变量并存储命令的输出,但这违反了其他规则。
命令直接放在 if
中并不会真正变得更长,您只是添加了 3 个字符。
if dpkg-query --show --showformat='${Status}\n' "$i" 2> \
/dev/null | grep "install ok installed" &> /dev/null
then
l_var_is_desktop="true"
fi