檢查RPM包是否已經安裝

kumu_linux發表於2012-07-17
###################RPM_CHECK1################# -->僅僅是檢查單個rpm包是否安裝,未安裝的不能自動安裝
#!/bin/bash
# check rpm is installed or not
uname -r
rpm --version

echo
P=${1?" must specify package name."}

rpm -qa "$P" > t1
my_size=$( wc -l < t1 )
echo " Size of report file is $my_size lines"

if [[ $( rpm -qa $P ) =~ ${P} ]]
# if [[ $( rpm -qa $P ) == *${P}* ]]
then
  echo " Package $P is installed."
else
  echo " Package $P not found."
fi

exit 0
###################RPM_CHECK1#################


###################RPM_CHECK2#################  -->可以實現多種rpm包的檢查,未安裝的自動安裝
#!/bin/bash
#filename: check_rpm
uname -r
rpm --version

check_rpm()
{
	while (( $# > 0 ))
	do
		if ! rpm -q $1 &> /dev/null;then
			yum install $1 -y &>/dev/null
			if	[ $? == 0 ];then
				echo "$1 install is success!"
			else
				echo "$1 is not in the systerm Packages!"
			fi
		fi
	shift #執行一次shift則去掉第一個引數,始終只需判斷$1即可  
	done
	exit 0
}

check_rpm $@
###################RPM_CHECK2################# 


###################RPM_CHECK3#################  -->結合以上兩個指令碼


#!/bin/bash
#filename: check_rpm
uname -r
rpm --version

P=${1?" must specify package name."}  #加入這一行表示必須需要輸入一個有效的軟體包名

check_rpm()
{
	while (( $# > 0 ))
	do
		if ! rpm -q $1 &> /dev/null;then
			yum install $1 -y &>/dev/null
			if	[ $? == 0 ];then
				echo "$1 install is success!"
			else
				echo "$1 is not in the systerm Packages!"
			fi
		else 
			echo "$1 is installed"
		fi
	shift #執行一次shift則去掉第一個引數,始終只需判斷$1即可  
	done
	exit 0
}
###################RPM_CHECK3################# 


相關文章