1

Maybe I am approaching this the wrong way, so I am open for suggestions. I am trying to avoid adding the output of INSTALLED_LIST to a file. I wish to use it within a variable.

Basically, I want to check if a list (INSTALLATION_PACKAGES) of packages are installed on a system. If not, record which ones failed to install, record the information, and exit. Outside of the if statement, this works fine (see commented out line), I am just having issues implementing it within an if statement.

Thx in advance for your help.

INSTALLED_LIST=`dpkg --list`

for i in $INSTALLTION_PACKAGES; do
  apt-get -y install $i
  # echo "$INSTALLED_LIST" | grep -ie "^ii  $i "
  if ! echo "$INSTALLED_LIST" | grep -ie "^ii  $i "  ; then
    echo $i >> $FAILED_INSTALL
  fi
done
  • 1
    You are approaching it the wrong way - INSTALLED_LIST's content isn't going to magically update itself after you did apt-get install. You should test either apt-get's exit status, or run dpkg -l $i | grep ... for each package. – muru Nov 9 '15 at 18:04
  • 1
    Thank you... I decided to check the exit status of apt-get install. – jdh239 Nov 9 '15 at 18:10
2

If you want all packages in $INSTALLATION_PACKAGES to be installed, just apt-get install all of them and then check whether any of them failed:

apt-get -y $INSTALLATION_PACKAGES
list=$(dpkg -l | grep ^ii)
for i in $INSTALLATION_PACKAGES do
    echo "$list" | grep -w "$i" || echo "$i" >> $FAILED_INSTALL
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.