\ /dev/null | grep "install-6ren">
gpt4 book ai didi

Bash 脚本命令和摆脱 shellcheck 错误 SC2181

转载 作者:行者123 更新时间:2023-12-05 02:14:59 29 4
gpt4 key购买 nike

我有以下 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 $?.

$

此警告的链接如下: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 脚本命令和摆脱 shellcheck 错误 SC2181,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52787152/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com