2018425日星期三

  實在是太感人了,這是我畢生寫的第一支shell,用來檢查我要的package是否有安裝在CentOS 機器上,由於CentOSrpm managerbase,所以就用 rpm –q <full_package_name> 以及 rpm –qa | grep <short_package_name> 來檢查package是否存在機器中,話不多說,咱就直接把script公布出來吧!!

 

# this script is to poc the checker of installed packages.

 

Vpackages=(httpd-tools-2.2.15-53.el6.centos.x86_64

httpd-2.2.15-53.el6.centos.x86_64

php-5.3.19-1.el6.remi.x86_64

php-mcrypt-5.3.19-1.el6.remi.x86_64

php-xml-5.3.19-1.el6.remi.x86_64

php-pdo-5.3.19-1.el6.remi.x86_64

php-fpm-5.3.19-1.el6.remi.x86_64

php-gd-5.3.19-1.el6.remi.x86_64

php-pear-1.9.4-12.el6.remi.noarch

php-pecl-memcache-3.0.5-4.el6.x86_64

php-common-5.3.19-1.el6.remi.x86_64

php-cli-5.3.19-1.el6.remi.x86_64

mod_fastcgi-2.4.6-2.el6.rf.x86_64

memcached-1.4.15-2.el6.remi.x86_64

mod_ssl-2.2.15-53.el6.centos.x86_64

openssl-1.0.1e-48.el6_8.3.x86_64

dejavu-lgc-sans-mono-fonts-2.33-1.el6.noarch

dejavu-sans-mono-fonts-2.33-1.el6.noarch)

 

Spackages=(wget gcc ntp vim telnet

bison gettext glib2 freetype fontconfig

libpng libpng-devel libX11 urw-fonts libX11-devel gl

ib2-devel libgdiplus libgdiplus-devel libexif glibc-devel

java unzip gcc-c++ automake autoconf

libtool make bzip2)

 

function checkVPackages() {

  local r1=`rpm -q $1`

  #echo $r1

 

  if [[ $r1 = *"is not installed"* ]] ; then # check if the package is not installed.

    local t=`echo "$1" | awk -F '-' '{print $1}'` # get the package short name to pass into 'rpm -qa' for query. eg.

    #echo "[debug]:  $t"

    echo "[debug]: List all the packages is related to $t:"

    echo "==== Start List ===="

    rpm -qa | grep $t

    echo "==== End List ===="

    return 0

  else

    return 1

  fi

 

}

 

function checkSPackages() {

  local r1=`rpm -q $1`

  #echo $r1

 

  if [[ $r1 = *"is not installed"* ]] ; then

    local r2=`rpm -qa | grep -m1 $1` # grep -m1 <object> : only return first line.

    echo "[debug]: $r2"

 

    if [ -n "$r2" ] ; then # check $r2 is not a null value.

      return 1

    else

      return 0

    fi

 

  else

    return 1

  fi

 

}

 

echo

echo "####check the packages being boundle version####"

 

for((i=0;i<${#Vpackages[@]};i++))

do

  checkVPackages ${Vpackages[$i]}

  #echo $?    # $? is the return value from shell function

  if [ $? -eq 1 ] ; then

    echo "${Vpackages[$i]} [OK]"

  else

    echo "${Vpackages[$i]} [NO]"

  fi

done

 

echo "#### End ####"

 

echo

echo "####check the packages without version####"

 

for((i=0;i<${#Spackages[@]};i++))

do

  checkSPackages ${Spackages[$i]}

  #echo $?    # $? is the return value from shell function

  if [ $? -eq 1 ] ; then

    echo "${Spackages[$i]} [OK]"

  else

    echo "${Spackages[$i]} [NO]"

  fi

done

 

echo "#### End ####"

 

以下是執行結果畫面:

文章標籤
創作者介紹
創作者 jackterrylau 的頭像
jackterrylau

儒道哲學的浪漫人生

jackterrylau 發表在 痞客邦 留言(0) 人氣()