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_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
smart_run_cmd() {
  echo $1
  if [ $? != 0 ]; then
    echo "Waaaah aborting"
    exit 1


Plain text

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