Two bash functions for interactive scripts

By devin, 18 November, 2014

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

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.