Two bash functions for interactive scripts

I just made two cool bash functions for use at work. One checks if a Debian/Ubuntu package is present and installs it if it isn't. The second takes a string bash command and echos it before running it, exiting the script if there's an error.

dependency() {
  PKG="$1"
  PKG_OK=$(dpkg-query -W --showformat='${Status}\n' ${PKG} | grep "install ok installed")
  if [ "" == "$PKG_OK" ]; then
    echo "No $PKG. Installing $PKG."
    sudo aptitude --assume-yes install $PKG
  fi
}
smart_run_cmd() {
  echo $1
  $1
  if [ $? != 0 ]; then
    echo "Waaaah aborting"
    exit 1
  fi
}
Tags: 

Add new comment