How can I check in a bash script, if some software is installed or not?

Viewed 6313

I want to check a list of software, if it is installed or not. if not, it should be displayed and the script should abort/exit. The output should look like following, if I execute the script once:

  wget is not installed
  telnet is not installed

Currently it's looking like following:

  wget is not installed

Execute script again...

  telnet is not installed

The current script is checking for installed software and aborts/exists, if the current checked software is not installed. That's not nice, because you have to run the script more times to identify and check, if each software is installed or not:

  LINUX_DISTRIBUTATION=$(grep -Eo "(Debian|Ubuntu|RedHat|CentOS)" /etc/issue)

  # Debian / Ubuntu
  if [ -f /etc/debian_version ] || [ "$LINUX_DISTRIBUTATION" == "Debian" ] || [ "$LINUX_DISTRIBUTATION" == "Ubuntu" ]; then
          declare -a NEEDED_SOFTWARE_LIST=(bash rsync wget grep telnet sed)

          for SOFTWARE in ${NEEDED_SOFTWARE_LIST[@]}; do
                  dpkg -l | grep -i $SOFTWARE | head -1 | if [[ "$(cut -d ' ' -f 1)" != "ii" ]]; then
                          echo -e "[ ${Red}FAILED ${RCol}]\t$SOFTWARE is NOT installed completely! Please install it...\n";
                          exit 1;
                  fi
          done
  # RedHat / CentOS
  elif [ -f /etc/redhat-release ] || [ "$LINUX_DISTRIBUTATION" == "RedHat" ] || [ "$LINUX_DISTRIBUTATION" == "CentOS" ]; then
          declare -a NEEDED_SOFTWARE_LIST=(bash rsync wget grep telnet sed)

          for SOFTWARE in ${NEEDED_SOFTWARE_LIST[@]}; do
                  if [[ "$(rpm -q $SOFTWARE)" == "package $SOFTWARE is not installed" ]]; then
                          echo -e "[ ${Red}FAILED ${RCol}]\t$SOFTWARE is NOT installed completely! Please install it...\n";
                          exit 1;
                  fi
          done
  else
          echo "[ ${Red}FAILED ${RCol}]\tYour system is currently not supported by this script.";
          exit 1;
  fi

I also think, that my solution is not the best. Can anybody adjust it? Thanks in advance! :)

Hint: I declared the variable "NEEDED_SOFTWARE_LIST" twice, because I thought I will need two "arrays" of software lists, because some distributions needs another software packages.

1 Answers
Related