有时候,我们不想在系统中安装rpm包,但是又需要这个rpm包的功能,那么可否实现呢? 这个需求是可以实现的,当我们用tar包去安装这个功能的时候就可以实现了.因为我们知道,rpm 包本质上是 运行了如下的过程:
#!/bin/bash
if [ $# -lt 1 ];then
echo -e "Usage:\n`basename $0` {RPM_PACKAGE_NAME} [OPTION]\n*************************************************************\nRPM_PACKAGE_NAME:\nThe rpm package name, this package must be installed already if you want to archive it. If you want to make portable version for this rpm package, then related .tar.gz file must be existing on /tmp/.\nOPTION:\nAvailable option value is : -i/-I. This option used for cp commands, it will come to interactive mode if need overwrite a existing file during release the RPM Packages. By default, it is '-n'.\n*************************************************************"
exit -1
fi
PKGNAME=$1
FLAG="n"
if [[ -n $2 ]];then
if [[ $2="-i" || $2="-I" ]];then
FLAG="i"
fi
fi
function OutTarPkg
{
set -e
mkdir -p /tmp/Free_${PKGNAME}_FileList
for i in `rpm -qR $PKGNAME | awk -F'[(]' '{print $1}' `;do rpm -q --whatprovides $i >/dev/null && echo $i;done >/tmp/Free_${PKGNAME}_FileList/deps.txt
rpm -qR $PKGNAME >/tmp/Free_${PKGNAME}_FileList/deps.source
set +e
for i in `rpm -ql $PKGNAME`;
do
n_path=`echo $i | sed "s#^#/tmp/Free_${PKGNAME}_FileList#"`
dir_name=`echo $n_path | awk -F'/' '{for(i=1;i<NF;i++)printf("%s/",$i)}'`
mkdir -p $dir_name
cp -L --preserve=all $i $n_path >/dev/null 2>/dev/null #This parts, not use -R, because it will copy all the related files under the directory.
done
cd /tmp
tar -czvf ./${PKGNAME}_RPM.tar.gz ./Free_${PKGNAME}_FileList/ >/dev/null
rm -rf /tmp/Free_${PKGNAME}_FileList
}
function DepsChk
{
for i in `cat ./deps.txt`;
do
rpm -q --whatprovides $i >/dev/null
if [ $? -ne 0 ];then
j=`echo $i | awk -F. '{print $1}'`
rpm -q --whatprovides $j >/dev/null
if [ $? -ne 0 ];then
echo "Dependency Required, no packages provides for: $i"
exit 1
fi
fi
done
echo "1. The Dependency verify results: PASS."
}
function ReleaseFile
{
cd /tmp
if [ ! -s /tmp/${PKGNAME}_RPM.tar.gz ];
then
echo -e "Please Verify the file: ${PKGNAME}_RPM.tar.gz exists in the path /tmp/.\nExit with code 2."
exit 2
fi
set -e
tar -xzvf ./${PKGNAME}_RPM.tar.gz >/dev/null
cd ./Free_${PKGNAME}_FileList
set +e
DepsChk
rm -f deps.txt
set -e
for i in `find . -iname "*" -type f | grep -Evi "deps.source|deps.txt" | sed 's/.//'`;
do
s_file=`echo $i | sed 's/^/./'`
if [ -f $i ];then
mv $i $i.`date +%Y%m%d_%H%M%S` #Rename first if the file exists already.
fi
mkdir -p ${i%/*} #Create the directory first if it not exist by default.
cp -L --preserve=all -${FLAG} ${s_file} $i
done
cd /tmp
rm -rf ./Free_${PKGNAME}_FileList
}
#Start from here:
rpm -qi $PKGNAME >/dev/null
if [ $? -eq 0 ];then
echo "System installed packages: $PKGNAME, start get the Tar file for this package."
OutTarPkg
if [ $? -eq 0 ];then
echo "Succeed get the tar.gz file for package: $PKGNAME on path /tmp/${PKGNAME}_RPM.tar.gz"
else
echo "Failed to get the tar.gz file."
exit 3
fi
else
echo "Package: $PKGNAME not found in this system, try to release the rpm related files from /tmp/${PKGNAME}_RPM.tar.gz"
ReleaseFile
if [ $? -eq 0 ];then
echo "2. Release the package files results on the system: Succeed.You can use it now."
else
echo "Failed to release the rpm package files."
exit 5
fi
fi