我想编写从数组中读取软件包列表并告诉我是否安装了相应 RPM 的代码:
ARRAY=(
pkg-config
python
python-devel
python-libs
readline
renderproto
sqlite
tcl
tk
zlib
)
for i in `echo ${ARRAY[@]}`
do
rpm -q $i
done
该代码无法正常工作。我希望输出是ok rpm named foobar is installed
or rpm named foobar is not installed
。
我怎样才能做到这一点?
答案1
该命令rpm
似乎会根据查询的包是否安装来更改其退出状态,因此可以通过以下方式使用它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