Why `set -o pipefail` gives different output even though the pipe is not failing

46 views Asked by At

I'm trying to check if a package is installed in a Ubuntu based system.

Running the following in my terminal returns true (exit code 0) since I have those packages:

dpkg --get-selections | grep -wq <package>

Where the <packages> are the following:

  • google-chrome
  • okular-extra-backends
  • scrcpy
  • xserver-xorg-video-vmware

When doing this in a script with set -o pipefail enabled, for some reason it fails for some of the packages and sometimes don't?

#!/usr/bin/env bash

set -eEo pipefail

check_package() {
  if ! dpkg --get-selections | grep -wq "$1"; then
    echo false
  else
    echo true
  fi
}

check_package google-chrome
check_package okular-extra-backends
check_package scrcpy
check_package xserver-xorg-video-vmware
  • I tried to remove -eE options, but the real culprit is -o pipefail
  • I test commenting out some of the function calls, when doing so, for some reason sometimes the function echoes true an other times echoes false. I don't get it. Here's some examples:

Example 1

check_package google-chrome
check_package okular-extra-backends
check_package scrcpy
check_package xserver-xorg-video-vmware

Result

false
false
false
true

Sometimes the result is this

false
false
true
true

Example 2

# check_package google-chrome
check_package okular-extra-backends
check_package scrcpy
check_package xserver-xorg-video-vmware

Result

false
true
true

Sometimes the result is this

false
false
true

What is happening there?

0

There are 0 answers