#!/bin/bash function usage { local BASECMD BASECMD=$(basename "$0") echo "usage: $BASECMD " echo 'idempotent debian package installation : update if necessary, install only if not yet done' exit 1 } [ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 2 [[ $# -lt 1 ]] && usage if [ "$(date --date='-12 hours' +%s)" -gt "$(date -d "$(stat -c %y /var/lib/apt/lists/partial)" +%s)" ]; then echo "updating repositoring..." apt-get update fi for i in "$@"; do if ! dpkg -l "$i" 2>/dev/null | grep -q ^ii; then sudo apt-get install -y "$i" elif [ -n "${VERBOSE+x}" ] && $VERBOSE; then echo "apt package <$i> already installed!" fi done