Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to write code that reads a list of packages from an array and tells me whether the corresponding RPM is installed:

ARRAY=(
pkg-config
python
python-devel
python-libs
readline
renderproto
sqlite
tcl
tk
zlib
)

for i in `echo  ${ARRAY[@]}`
do
    rpm -q $i
done

This code doesn't work properly. I want the output to be ok rpm named foobar is installed or rpm named foobar is not installed.

How can I do this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The command rpm appears to change its exit status depending on whether the queried package is installed, so it can be used by if:

for package in "${ARRAY[@]}"; do
    if rpm -q $package >/dev/null 2>/dev/null; then
       echo "Package $package is installed."
    else
       echo "Package $package is not installed."
    fi
done
share|improve this answer
    
Works fine,a suggestion only 2>&1 >/dev/null is better than two > > –  elbarna May 5 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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