0

I am trying to improve a build script (bash or Makefile) to install some prerequisites if necessary.

I do have a long list of packages (10-15) and I do want to install them only if they are not installed.

As the build script may be run by non root I do want to run sudo yum install only once and only if needed (at least one package is not installed).

My current implementation is clearly not optimal:

for PACKAGE in a b c ... ; do 
    yum list installed $PACKAGE >/dev/null 2>&1 || sudo yum install -q -y $PACKAGE ;
done 

How can I do this?

sorin
  • 161,544
  • 178
  • 535
  • 806
  • Yum can get a list of packages - did you try it directly without the loop? – cohenjo Oct 19 '16 at 19:34
  • If you give it more than one package name as parameter it does not return an error code if one of the specified packages is missing. For example "a b" will return success (0) if a is installed and b is not, making it useless. – sorin Oct 19 '16 at 19:37

1 Answers1

0

You could, of course, just put the list of installed packages into a file and run grep against that in a loop, but should you wish to avoid creating any files, running rpm should work:

for p in a b c ... ; do 
  rpm -q > /dev/null 2>&1 $PACKAGE || sudo yum install -q -y $p
done 

When querying package information, rpm returns zero when the package is found and non-zero otherwise.