Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Shell 程式設計

Shell 編程:查找套件相依檔案所在的 RPM 包

當你想知道某個相依檔案是由哪個 RPM 包提供時,可用以下腳本在指定目錄中逐一比對。假設包所在目錄為 /media/server。

#!/bin/bash
# This script is used to query the package dependencies.

function query() {
    LINE=0
    PACKET=null
    MAX=`ls $2/*.rpm | wc -l`
    while true
    do
        LINE=`expr $LINE + 1`
        if [ $MAX -lt $LINE ]
        then
            echo "Not find any package."
            unset LINE PACKET MAX
            return 1
        fi
        PACKET=`ls $2/*.rpm | sed -n ${LINE}p`
        rpm -pql $PACKET 2>/dev/null | grep -v "warning:" 2>/dev/null | grep $1 &>/dev/null
        if [ $? = 0 ]
        then
            echo $PACKET
            unset LINE PACKET MAX
            return 0
        fi
    done
}

if [ $# != 1 ]
then
    echo "Must have a parameter."
    echo "Usage: "$0" parameter"
    exit 1
fi

PACKET_DIR=/media/Server
DEPEND_FILE=$1
MESSAGE=null

echo "Querying, please wait ..."
if MESSAGE=`query $DEPEND_FILE $PACKET_DIR`
then
    echo "Query is completed."
    echo "File where the package is:"
    echo "     "$MESSAGE
    unset PACKET_DIR MESSAGE DEPEND_FILE query
    exit 0
else
    echo "Query is completed."
    echo $MESSAGE
    unset PACKET_DIR MESSAGE DEPEND_FILE query
    exit 1
fi

執行範例:

[root@localhost Desktop]# ./example17.4.sh libapr-1.so.0
Querying, please wait ...
Query is completed.
File where the package is:
      /media/Server/apr-1.2.7-11.i386.rpm

腳本用 rpm -pql 列出每個 RPM 內含的檔案,再以 grep 比對目標檔名,找到後印出對應的套件檔。找不到時也會明確提示,方便逐一排查相依關係。

Copyright © Linux教程網 All Rights Reserved