徹底解除安裝rpm軟體包的指令碼

李先靜發表於2020-04-06

徹底解除安裝rpm軟體包的指令碼

 

有時解除安裝一個軟體包時,發現有很多其它軟體包依賴它,強制解除安裝會造成這些軟體包都無法使用。下面這個指令碼可以解除安裝一個軟體包及依賴於它的所有軟體包。

 

rpm_erase_deps.sh

 

#!/bin/bash

 

#######################################################

 

PACKAGE=$1

RPM_OPS="-e"

 

#######################################################

 

if [ "$1" = "" ]

then

       echo "usage  : $0 package --rpmops=[/'opts/']"

       echo "example: $0 ant --rpmops='--test -e'"

       exit 1

fi

 

#######################################################

 

while [ ! "$1" = "" ]

do

       shift

       arg=$1

       if [ "${arg:0:8}" = "--rpmops" ]

       then

              RPM_OPS="${arg:9:100}"

       fi

done

 

function show_depends()

{

       local _PACKAGE="$1"

       local _INDENT="$2"

             

       DEPS=`rpm --test -e $_PACKAGE 2>&1|grep needed|awk '{print $NF}'`

      

       for depends in $DEPS

       do

              echo "$_INDENT $depends"

              show_depends "$depends" "$_INDENT""--"

       done

}

 

function rpm_do_erase()

{

       local _PACKAGE="$1"

       local _INDENT="$2"

             

       DEPS=`rpm $RPM_OPS $_PACKAGE 2>&1|grep needed|awk '{print $NF}'`

      

       for depends in $DEPS

       do

              echo "erase $_INDENT $depends"

              rpm_do_erase "$depends" "$_INDENT""--"

 

              if [ ! "$?" = 0 ]

              then

                     echo "erase $_INDENT $depends failed."

              fi

       done

}

 

echo "show depends graph:"

echo "$PACKAGE"

show_depends $PACKAGE "--"

 

echo "do you want to uninstall $_PACKAGE (y/n)?"

read yn

 

if [ "$yn" = "y" ]

then

       rpm_do_erase $PACKAGE "--"

else

       echo "cancel uninstall $PACKAGE"

fi

 

相關文章