Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ensure yum install was successful in a shell script?

Tags:

bash

shell

yum

I have a shell script which checks if there is an internet connection (by pinging google), and then calls

yum install packageA packageB --assumeyes

How would I confirm that the packages were installed (or were already installed)? Do I make another yum call and parse the output (I presume this gets very complicated if the system is in another language)?

like image 312
jedierikb Avatar asked Dec 09 '11 14:12

jedierikb


2 Answers

Based on this random post, it looks like yum returns an error code to the shell. You can test this out by running a command and then immediately (as the next command) doing:

echo $?

That will print the previous command's return code. Success should be 0, failure of some kind nonzero. But that's just a guess since I don't have a box accessible to me at the moment. :)

like image 131
Dan Fego Avatar answered Oct 11 '22 09:10

Dan Fego


I've used the following method, which might not be foolproof, but seems to work:

Assuming the variable PACKAGES contains the list of packages you want to install, then:

  1. Run yum -y install $PACKAGES (I assume if this is a script, you really want to pass -y to avoid prompting).
  2. Check its exit status in order to detect some failure conditions.
  3. Run rpm --query --queryformat "" $PACKAGES, which will output nothing for each package that was installed successfully, and will output package <name> is not installed for each failure.
  4. Check its exit status, which appears to be the number of packages that were not successfully installed, i.e. will be 0 on success as usual.

This will only work if PACKAGES contains plain package names that yum is expected to find in a repository, not if it contains other things that yum accepts like URLs, file names or Provides: names.

like image 34
doshea Avatar answered Oct 11 '22 11:10

doshea