您的位置:首页 > 运维架构 > Shell

Shell实现自动更新目录名称与文件中版本号信息与时间戳

2015-12-19 19:11 591 查看
背景:

修改如下样例目录下所有目录的版本号信息与时间戳;

修改如下样例文件中的版本号信息与时间戳;

样例目录:

$ find template_patch_dir/
template_patch_dir/
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE11_D2015-12-10
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE11_D2015-12-10/modules
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE11_D2015-12-10/package.xml
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE10_D2015-12-10
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE10_D2015-12-10/modules
template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE10_D2015-12-10/package.xml


样例文件信息:

$ cat template_patch_dir/ONIP_SNE_V300R002C80CP1006_SUSE11_D2015-12-10/package.xml
version="V300R002C80CP1006"

name="ONIP_SNE_V300R002C80CP1006_SUSE11_D2015-12-10.tar.gz"


实现Shell脚本:

#!/bin/sh

template_version="V300R002C80CP1006"
template_date="2015-12-10"

##make os type pkg
function make_os_type_pkg
{
cd ${current_path}
os_type=$1
old_dirname=`ls | grep ${os_type}`
new_dirname="ONIP_SNE_${new_version}_${os_type}_D`date +%Y-%m-%d`"

mv ${old_dirname} ${new_dirname}

cd *${os_type}*
sed -i "s#${template_version}#${new_version}#g" package.xml
sed -i "s#${template_date}#`date +%Y-%m-%d`#" package.xml

tar zcf ${new_dirname}.tar.gz *
###delete dir and file name not contents '.gz'
ls | grep -Fv '.gz' | xargs rm -rf

}

function main
{
if [ $# -ne  1 ];then
echo "make_patch_pkg.sh new_version_number"
exit 1
fi

new_version=$1

####copy example pkg, not modify the example dir
rm -rf new_patch_dir
cp -r template_patch_dir new_patch_dir
cd temp_dir

current_path=`pwd`

make_os_type_pkg SUSE10
make_os_type_pkg SUSE11
}

main $@


执行结果:

$ ./make_patch_pkg.sh  V300R002C30CP1011B06
[xbh@bogon xbh]$ ls
make_patch_pkg.sh  new_patch_dir  template_patch_dir
[xbh@bogon xbh]$ find new_patch_dir/
new_patch_dir/
new_patch_dir/ONIP_SNE_V300R002C30CP1011B06_SUSE11_D2015-12-19
new_patch_dir/ONIP_SNE_V300R002C30CP1011B06_SUSE11_D2015-12-19/ONIP_SNE_V300R002C30CP1011B06_SUSE11_D2015-12-19.tar.gz
new_patch_dir/ONIP_SNE_V300R002C30CP1011B06_SUSE10_D2015-12-19
new_patch_dir/ONIP_SNE_V300R002C30CP1011B06_SUSE10_D2015-12-19/ONIP_SNE_V300R002C30CP1011B06_SUSE10_D2015-12-19.tar.gz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: