前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享一个shell脚本用于“基于已安装的rpm包,创建tar压缩包”

分享一个shell脚本用于“基于已安装的rpm包,创建tar压缩包”

作者头像
qsjs
发布2020-06-09 10:43:17
6870
发布2020-06-09 10:43:17
举报
文章被收录于专栏:MyPanda的学习笔记

有时候,我们不想在系统中安装rpm包,但是又需要这个rpm包的功能,那么可否实现呢? 这个需求是可以实现的,当我们用tar包去安装这个功能的时候就可以实现了.因为我们知道,rpm 包本质上是 运行了如下的过程:

  1. 运行 pre-script, 进行依赖检查等.
  2. 释放文件到相应的目录.
  3. 运行 post-script 等. 忽略pre-script 和 post-script, 那么其实就是文件的release 过程,所以可以把相应文件收集起来,然后打包就可以了.
下面的脚本在系统中没有安装 对应rpm包的时候,会查找对应的 tar包,然后释放到系统中,如果系统中已经安装了对应的rpm包,那么会打包生成相应rpm的tar 包. 如果把这个tar包放到目标系统中,那么目标系统需要和rpm包所在的系统是相同的.
代码语言:javascript
复制
#!/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
本文原创,转载请著名出处.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下面的脚本在系统中没有安装 对应rpm包的时候,会查找对应的 tar包,然后释放到系统中,如果系统中已经安装了对应的rpm包,那么会打包生成相应rpm的tar 包. 如果把这个tar包放到目标系统中,那么目标系统需要和rpm包所在的系统是相同的.
  • 本文原创,转载请著名出处.
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档