Skip to content

Instantly share code, notes, and snippets.

@labbots
Last active December 22, 2016 16:20
Show Gist options
  • Save labbots/c2a2d342ab6b1825ab5552b1454e8a61 to your computer and use it in GitHub Desktop.
Save labbots/c2a2d342ab6b1825ab5552b1454e8a61 to your computer and use it in GitHub Desktop.
Script to install Ubuntu packages from package list file. If the package fails to install then report them in separate log file for manual installation. https://labbots.com/how-to-migrate-from-one-ubuntu-machine-to-another/
#!/bin/bash
#!/bin/bashset -e
VERBOSE=true
FILE="$1"
function log() {
error="$2"
if [[ "$VERBOSE" = true ]] || [[ "$error" = true ]] ; then
echo -e "${1}"
fi
}
debInstalled() {
dpkg-query -Wf'${db:Status-abbrev}' "$1" 2>/dev/null | grep -q '^i'
}
# Check whether the script is run as root
if [[ $UID -ne 0 ]];then
log "Script needs to be run as root.Aborted with no further action." true
exit 1
fi
if [ -z "$FILE" ];then
log "Filepath of package list is mandatory." true
exit 1
fi
if [ ! -f "$FILE" ];then
log "Invalid file name : $FILE" true
exit 1
fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROGRAMNAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
PROGRAMNAME=${PROGRAMNAME%.*}
LOGFILE="$DIR/$PROGRAMNAME.log"
ERRLOGFILE="$DIR/$PROGRAMNAME.error"
NOTINSTALLEDLIST="$DIR/$PROGRAMNAME-not-installed"
#update package manager
apt-get update
#read from package list file and
#install packages if it is not already installed
while IFS='' read -r line || [[ -n "$line" ]]; do
if debInstalled "$line"; then
log "$line - package _is_ installed!"
else
log "$line - Installing package...!\n"
apt-get install -q -y --force-yes $line >> "$LOGFILE" 2>>"$ERRLOGFILE"
if [[ $? > 0 ]] ;then
echo "$line" >> "$NOTINSTALLEDLIST"
fi
fi
done < "$FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment