Shell | 快速大量查找未安装的rpm软件包脚本

此shell脚本实现,快速查找rpm方式安装的软件包
编写原因:Oracle安装时需要查找所需的未安装软件包,查找起来十分不便。
脚本内容:最初第一次写的没想太多,只想快速找到未安装的rpm即可,所以速度比较慢,因为每次都需要执行一次“rpm -qa|grep 包名”的命令,太影响效率了。后台修改成执行一次rpm -qa(查找所有已安装rpm包)并写入rpm-qa.temp文本,以后只需在改文本内查找即可!速度提升几乎百倍
下载地址:(QQ中转站

将要查找的所有的包都保存到temp.txt文件内:格式如下

gcc-4.1.1
gcc-c++-4.1.1
glibc-2.5-12
glibc-2.5-12 (32 bit)
glibc-common-2.5

未命名

使用方法:
1)复制该博文内容,保存到你Linux环境下,并"chmod 755 该文件"具有可执行权限";
2)将要查找的所有的包都保存到temp.txt文件内:格式如下

gcc-4.1.1
gcc-c++-4.1.1
glibc-2.5-12
glibc-2.5-12 (32 bit)
glibc-common-2.5

3) 执行" ./该文件"即可
未添加:可以使用"cat > temp.txt <<!"方式来输入须查找包名(当输入“!”结束,在执行./该文件)

#///////////////////////////////////////////////////////////////////////#
#/                                                                      #
#/  Welcome Browes:
http://www.kinggoo.com                              #
#/  Find Oracle 11g R1 Required Packages                                #
#/  Add To temp File Query                                              #
#///////////////////////////////////////////////////////////////////////#
#/  Url:
http://www.kinggoo.com/archives/chengxu-shell-rpm-qa-grep.html  #
#///////////////////////////////////////////////////////////////////////#

 
#!/bin/bash
rm -fr rpm-qa.log > /dev/null
rm -fr cat_temp > /dev/null
clear
printf "\n"
echo -e "\e[1;49;36mMy Name is\e[0m  \e[1;5;49;32mKingGoo\e[0m!    "
echo -e "\e[1;49;36mWebSite:\e[0m \e[49;37mhttp://www.kinggoo.com \e[0m"
printf "\n"
#Optimize Speed
rpm -qa > rpm-qa.temp
#cat temp.txt |cut -d ‘-‘ -f 1 > cat_temp
awk -F ‘-‘ ‘{printf $1}{ printf "-"}{ printf $2}{printf "-"}{printf $3"\n"}’ temp.txt > cat_temp
l=`cat cat_temp |wc -l`
i=1
while [[ "$i" -le "$l" ]];do
    H_ZHI=`awk -F ‘-‘ ‘NR==’$i'{ printf $1 }’ cat_temp`
    test -z "$H_ZHI"
    Statu_TEST_H_ZHI=$?
    if (( $Statu_TEST_H_ZHI > 0 ));then
        H_ZHI1=`awk -F ‘-‘ ‘NR==’$i'{ printf $2 }’ cat_temp`
        test -z "$H_ZHI1"
        Statu_TEST_H_ZHI1=$?
        if (( $Statu_TEST_H_ZHI1 > 0 ));then
            CUT_H_ZHI1=`echo $H_ZHI1|cut -c 1`
            expr $CUT_H_ZHI1 + 10 > /dev/null 2> /dev/null
                       Statu_CUT_H_ZHI1=$?
            if (( $Statu_CUT_H_ZHI1 > 0 ));then
                CUT_H_ZHI1=-$CUT_H_ZHI1.*
                H_ZHI2=`awk -F ‘-‘ ‘NR==’$i'{ printf $3}’ cat_temp`
                test -z "$H_ZHI2"
                Statu_TEST_H_ZHI2=$?
                if (( $Statu_TEST_H_ZHI2 > 0 ));then
                    CUT_H_ZHI2=`echo $H_ZHI2|cut -c 1`
                    expr $CUT_H_ZHI2 + 10 > /dev/null 2> /dev/null
                    Statu_CUT_H_ZHI2=$?
                    if (( $Statu_CUT_H_ZHI2 > 0 )) ; then
                            CUT_H_ZHI2=-$CUT_H_ZHI2.*
                    else
                            CUT_H_ZHI2=""
                            echo -n .
                    fi
                else
                    CUT_H_ZHI2=""
                fi
            else
                CUT_H_ZHI1=""   
                CUT_H_ZHI2=""
                echo -e -n "\e[5;32m.\e[0m".
            fi
        else
            CUT_H_ZHI1=""
            CUT_H_ZHI2=""
        fi
#Delete Every "rpm -qa" Query.   
#        rpm -qa |grep -i "^$H_ZHI$CUT_H_ZHI1$CUT_H_ZHI2" 1> /dev/null
#Query "rpm -qa > rpm-qa.temp" File.
        grep -i "^$H_ZHI$CUT_H_ZHI1$CUT_H_ZHI2" rpm-qa.temp 1> /dev/null
        Statu0=$?
        if (( $Statu0 > 0 ));then
            ERROR=`awk ‘NR==’$i'{printf $1 }’ temp.txt`
            echo "$ERROR" >> rpm-qa.log
        fi
    fi
    (( i++ ))
done
clear
printf "\n"
echo -e "\e[49;36mMy Name is\e[0m  \e[1;5;49;32mKingGoo\e[0m!   "
echo -e "\e[49;36mWebSite:\e[0m \e[49;37mhttp://www.kinggoo.com \e[0m"
echo " "
test -f rpm-qa.log
Statu1="$?"
if [ $Statu1 -eq "0" ];then
    echo -e "\e[1;5;41;7mPlease, Install Below Package\e[0m"
           cat rpm-qa.log
    rm -fr rpm-qa.log > /dev/null
else
           echo -e "\e[1;42mAlready full installation\e[0m"
fi

printf "\n"
rm -fr rpm-qa.temp > /dev/null
rm -fr cat_temp > /dev/null
exit

- THE END -
版权声明:
转载原创文章请注明,文章出处://kinggoo.com
原文地址:https://kinggoo.com/chengxu-shell-rpm-qa-grep.htm
发表评论?

4 条评论。

  1. 博主的文章很好,学习了,欢迎回访

回复给 小 天 ¬
取消回复


此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据