How can I check for apt-get errors in a bash script?
The simplest approach is to have your script continue only if apt-get
exits correctly. For example:
sudo apt-get install BadPackageName &&
## Rest of the script goes here, it will only run
## if the previous command was succesful
Alternatively, exit if any steps failed:
sudo apt-get install BadPackageName || echo "Installation failed" && exit
This would give the following output:
terdon@oregano ~ $ foo.sh
[sudo] password for terdon:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package BadPackageName
Installation failed
This is taking advantage of a basic feature of bash and most (if not all) shells:
&&
: continue only if the previous command succeeded (had an exit status of 0)||
: continue only if the previous command failed (had an exit status of not 0)
It is the equivalent of writing something like this:
#!/usr/bin/env bash
sudo apt-get install at
## The exit status of the last command run is
## saved automatically in the special variable $?.
## Therefore, testing if its value is 0, is testing
## whether the last command ran correctly.
if [[ $? > 0 ]]
then
echo "The command failed, exiting."
exit
else
echo "The command ran succesfuly, continuing with script."
fi
Note that if a package is already installed, apt-get
will run successfully, and the exit status will be 0.
Related videos on Youtube
![HarlemSquirrel](https://i.stack.imgur.com/AH6Ys.jpg?s=256&g=1)
HarlemSquirrel
Updated on September 18, 2022Comments
-
HarlemSquirrel almost 2 years
I am writing a bash script to install software and update Ubuntu 12.04. I would like the script to be able to check for apt-get errors especially during apt-get update so that I can include corrective commands or exit the script with a message. How can I have my bash script check for these types of errors?
Edit March 21st: Thank you terdon for the providing just the information I needed! Here is the script I created with a combination of your suggestions to check for updates and recheck when errors occur and then report back. I will be adding this to a longer script that I am using to customize new Ubuntu installations.
#!/bin/bash apt-get update if [ $? != 0 ]; then echo "That update didn't work out so well. Trying some fancy stuff..." sleep 3 rm -rf /var/lib/apt/lists/* -vf apt-get update -f || echo "The errors have overwhelmed us, bro." && exit fi echo "You are all updated now, bro!"
-
Registered User over 10 yearsI would rather suggest you to use corrective commands themselves if they work in normal conditions.
-
terdon over 10 yearsWhat type of errors?
-
jarno over 8 yearsI suppose you don't have to repeate
-f
option forrm
, so use e.g.rm -rfv /var/lib/apt/lists/*
, instead.
-
-
HarlemSquirrel over 10 yearsThis is what I was looking for! I think I will use the if command so I can have update try again. Thank you!
-
HarlemSquirrel over 10 yearsI just edited my question to give you credit and clicked the check mark. I will now look over my previous questions. Thanks again!