5

I have the following bash script:

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

and the ShellCheck utility (https://www.shellcheck.net/) is giving me the following output:

$ shellcheck myscript

Line 17:
    if [[ $? -eq 0 ]]; then
          ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

$ 

The link to this warning is the following: https://github.com/koalaman/shellcheck/wiki/SC2181

What is the best way for modifying this. The command is really too long to put into one line. I would like to avoid using ShellCheck ignore rules.

I've tried creating a local variable and storing the output of the command, but this breaks other rules.

1 Answer 1

5

The command doesn't really get much longer by putting it directly in if, you're just adding 3 characters.

if dpkg-query --show --showformat='${Status}\n' "$i" 2> \
    /dev/null | grep "install ok installed" &> /dev/null
then
    l_var_is_desktop="true"
fi
2
  • 1
    I wanted to keep the conditional small in term of length of characters, but for right now I am going to just use what you suggested. Thank you.
    – Kris
    Commented Oct 15, 2018 at 13:50
  • You have to make a choice between appeasing shellcheck.net and your personal code style esthetics.
    – Barmar
    Commented Oct 15, 2018 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.