0

I am trying to find out if certain RPM packages exists in an array. if any of the package is missing it will exit with the message "Package doesnt exist" .Here is my code

#!/bin/bash


 echo "Checking for Pre-requisites X11 libraries required for installation"
 my_array=(xorg-x11-drivers xorg-x11-utils xorg-x11-xauth xorg-x11-xinit initial-setup-gui initial- 
 setup-gui install initial-setup-gui glx-utils )

 for (( i = 0; i < ${#my_array[@]} ; i++ )); do
 #printf "\n**** checking: ${my_array[$i]} *****\n\n"
 if  rpm ! -qa  "${my_array[$i]}"
 then
 echo "Package missing"
 exit 1
 else
 echo "All Package available"
 exit
 fi

done

But this one seems to do the opposite that is exiting on finding a matching package. Can someone pls let me know how i am messing up here ?

0

Your code looks incomplete, you're missing a closing done for your for loop. If the if clause is within your loop, the exit commands are going to make the whole script stop (seeing as you have this on both sides of the else, it will always stop after the first item.)

Interating the array like that will probably work, but there are easier ways to go about it. For example (untested):

echo "Checking for Pre-requisites X11 libraries required for installation"
my_array=(xorg-x11-drivers xorg-x11-utils xorg-x11-xauth xorg-x11-xinit initial-setup-gui initial-setup-gui install initial-setup-gui glx-utils )
missing=()

for package in "${my_array[@])"; do
  rpm -q "${package}" || missing+=("${package}")
done

if [ ${#missing[@]} -gt 0 ] ; then
  echo "Packages missing: ${missing[@]}"
  exit 1
else
  echo "All Packages available"
  exit
fi
1
  • Thanks for pointing out. Actually i had missed putting it out in the question here but it was there in the actuall code. But still many thanks for the help. Ur code worked like charm
    – achak01
    Mar 17 '20 at 14:57
1

I see 2 issues:

  1. rpm -qa , does it really take an argument ? rpm -q does.
  2. rpm ! -qa , does not work on the shell ! rpm -qa does.
1
  • rpm -qa does not take any arguments and rpm ! is invalid.
    – BulletBob
    Mar 17 '20 at 14:13

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.