Bash

在 shell 腳本中迭代數組

  • May 5, 2015

我想編寫從數組中讀取包列表並告訴我是否安裝了相應的 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 installedor rpm named foobar is not installed

我怎樣才能做到這一點?

該命令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

引用自:https://unix.stackexchange.com/questions/200635