0

When running sudo apt-get update -q, sometimes I see things like

Err:23 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu precise/main i386 Packages
  Could not connect to ppa.launchpad.net:80 (91.189.95.83), connection timed out
Err:24 http://ppa.launchpad.net/chris-lea/redis-server/ubuntu precise/main Translation-en
  Unable to connect to ppa.launchpad.net:http:

or like

W: http://us-central1.gce.archive.ubuntu.com/ubuntu/dists/precise-updates/InRelease: Signature by key 630239CC130E1A7FD81A27B140976EAF437D05B5 uses weak digest algorithm (SHA1)

But echo $? tells me that apt-get update -q succeeded. How can I get the error/warning status to be fatal, and make apt-get update fail with a non-zero exit code?

Jason Gross
  • 105
  • 6
  • 1
    Hmmmm, if I run it with an error my echo $? returns 100. Without errors it returns 0. – Terrance Jan 16 '18 at 16:54
  • Hmmm, maybe I was misreading the travis logs I was looking at... What about when there are warnings? – Jason Gross Jan 16 '18 at 21:40
  • 1
    Look at https://unix.stackexchange.com/questions/175146/apt-get-update-exit-status in the answer there is a little script that could detect both W: and E:. But when I did the apt update -q with just warnings, I got 0 on the echo $?. But the script passed and got both errors and warnings. – Terrance Jan 16 '18 at 22:18

2 Answers2

1

You could create a bash script that check the command output :

if { apt-get update 2>&1 || echo E: update failed; } > result.txt; cat result.txt | grep -q '^[(W)|(E]:'; then
    echo error
else (
        if  cat result.txt | grep -q '^[(Get)]:'; then
            echo repaired
        else 
            echo success
        fi
    )
fi

Here, we check if a line start with E (Err: or E:, etc..) for errors or with W for warnings. Both return the status code error.

If there is no error and there is a Get:, it means that we update our repos. It returns the status code repaired.

And finally, if we havent encountered any of those, the script was a success.

1

As mentioned by @Terrance, this question is very close to that other question apt-get update exit status (in Unix & Linux StackExchange).

FYI I've just posted there another solution that can also be combined with the travis_retry Travis CI command (which might be the intended use case of the OP):

exec {fd}>&2 # copy stderr to some unused fd
travis_retry bash -o pipefail -c "sudo apt-get update -y -q 2>&1 | tee /dev/fd/$fd | ( ! grep -q -e '^Err:' -e '^E:' )"
exec {fd}>&- # close file descriptor
ErikMD
  • 124