How can I get `apt-get update` to reflect error status in its exit code?












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?










share|improve this question


















  • 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 unix.stackexchange.com/questions/175146/… 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
















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?










share|improve this question


















  • 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 unix.stackexchange.com/questions/175146/… 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














0












0








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?










share|improve this question













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?







apt updates error-handling aptitude






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 16 '18 at 16:41









Jason Gross

1034




1034








  • 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 unix.stackexchange.com/questions/175146/… 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














  • 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 unix.stackexchange.com/questions/175146/… 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








1




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




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




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




1




Look at unix.stackexchange.com/questions/175146/… 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




Look at unix.stackexchange.com/questions/175146/… 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 Answers
2






active

oldest

votes


















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.






share|improve this answer





























    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





    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f996665%2fhow-can-i-get-apt-get-update-to-reflect-error-status-in-its-exit-code%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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.






      share|improve this answer


























        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.






        share|improve this answer
























          1












          1








          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 24 '18 at 14:31









          Quentin Pétel

          112




          112

























              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





              share|improve this answer


























                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





                share|improve this answer
























                  1












                  1








                  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





                  share|improve this answer












                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 13 '18 at 12:53









                  ErikMD

                  1113




                  1113






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f996665%2fhow-can-i-get-apt-get-update-to-reflect-error-status-in-its-exit-code%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How did Captain America manage to do this?

                      迪纳利

                      南乌拉尔铁路局