Shell脚本根据spec文件生成rpm包安装rpm包并检查文件属性-工具

本文介绍了一个用于自动化构建及验证RPM软件包的Shell脚本。该脚本能够处理从源代码路径构建RPM包到安装并验证文件属性的一系列流程。它首先检查所需的工具是否安装,然后构建RPM包,接着安装该包,并最后根据SPEC文件中定义的文件属性来验证已安装文件的属性。
#!/bin/bash
#filename: rpm_miv.sh


#Use this shell script to :
#1. make rpm package
#2. install rpm package
#3. Verify the files' attributes which installed by the rpm package baaed on spec file


if [ $# -ne 1 ]; then
echo "Usage: $0 full source code path";
exit -1
fi


if [ ! -d $1 ]; then
echo source code path : $1 is not existing !!!
exit -1
fi


cd $1
find . -name "*.spec" > /dev/null
if [ $? -ne 0 ]; then
echo NO sepc file defined in the soource code !!!
exit -1
fi


rpm -q make > /dev/null
if [ $? -ne 0 ]; then
echo 'install make tool...'
yum -y install make
    if [ $? -ne 0 ]; then
        echo 'install make tool failed!'
        exit -1
    fi
fi


rpm -q rpm-build > /dev/null
if [ $? -ne 0 ]; then
echo 'install rpm-build tool...'
yum -y install rpm-build
    if [ $? -ne 0 ]; then
        echo 'install rpm-build tool failed!'
        exit -1
    fi
fi


#Generate the rpm package
make


#install the rpm package
#cd output/rpm
cd output
rpm_file=`find . -name "*.rpm"`
rpm=`echo $rpm_file | sed 's/.\///' | sed 's/.rpm//'| sed 's/\// /' |awk '{print $NF}'`
echo $rpm
#if the rpm package has been installed, then remove it firstly
rpm -q $rpm > /dev/null
if [ $? -eq 0 ]; then
    echo "remove installed $rpm package..."
    rpm -e $rpm
    if [ $? -ne 0 ]; then
        echo "remove installed $rpm package failed!"
        exit -1
    fi
fi


#install the rpm package
rpm -ivh $rpm_file
if [ $? -ne 0 ]; then
    echo "Install $rpm package failed!"
    exit -1
else
echo "Install $rpm package success!"
fi


echo $1
cd $1
#get out files defattr from spec file
spec_file=`find . -name "*.spec"`
echo $spec_file
filesnum=`sed -n "/%files/=" $spec_file`
defattrnum=`expr $filesnum + 1`
defattr=`sed -n "${defattrnum}p" $spec_file`
printf "Defined file attributes: %s in %s\n" $defattr $spec_file


def_mod=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $1}'`
def_mod=`echo $def_mod | sed 's/0//'`
def_user=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $2}'`
def_group=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $3}'`
printf "%s %s %s\n" $def_mod $def_user $def_group


#compare defattr with file attr by stat command in the installed files list
i=0
flag=0
while read line;
do
let i+=1
if [ $i -gt $defattrnum ] && [ -n "$line" ];
then
printf "Checking %s...\n" $line 
if [ ! -f $line ];
then
flag=1
printf 'File: %s is not existing!\n' $line
break
fi
linenum_all=`stat $line | sed -n "/Access/="`
line1=`echo $linenum_all | awk '{print $1}'`
content=`stat $line | sed -n "${line1}p"`
echo $content
mod=`echo $content | awk '{print $2}' | sed 's/\/-[rwx-]*)//' \
| sed 's/(0//'`
user=`echo $content | awk '{print $6}' | sed 's/)//'`
group=`echo $content | awk '{print $10}'| sed 's/)//'`


printf "%s %s %s\n" $mod $user $group
if [ "$def_mod" != "$mod" ] || [ "$def_user" != "$user" ] || [ "$def_group" != "$group" ];
then
flag=1
break
fi
fi
done < $spec_file


#print out check result
if [ $flag -ne 1 ];
then
     echo 'All Passed!'
     exit 0
else
     printf "Failed @ file: %s\n!" $line
     exit 1
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值