3

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.

Kris
  • 363
  • 3
  • 11

1 Answers1

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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 Oct 15 '18 at 13:50
  • You have to make a choice between appeasing `shellcheck.net` and your personal code style esthetics. – Barmar Oct 15 '18 at 15:32