Practical application shell list of dictionary operations

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/weixin_37038498/article/details/89174944

the shell using lists and dictionaries realization of multi-file decompression

Depending on the work you want to extract the files to a different folder, so write a script deal with these things

1. Preparations

First, to have a unified naming format, this is a prerequisite to write the script, or just a bunch of strange names package deal you enough headaches
following naming format: in line with a similar format to

v6.1_aiwobu.zip
v6.2_aiwobu.zip
v6.1_buaini.zip

2. write the script

Knowledge Point 2.1 used

  1. Generating a list of operation
  2. Dictionary generating operation
  3. Biography participate function's return value
  4. Other basics: sed, awk, etc.

2.2 detailed script

#!/bin/bash
#用途:	1.根据文件名解压拷贝web文件到指定位置,并更改版本号
#		2.解压服务文件到指定位置
#原理:	1.分别获取版本号,存储在列表中
#		2.将列表生成字典,方便取用
#作者:	connor
#时间:	xxxx.2.14
#版本:	v2
source_dir="/data/source"
web_dir="/data/clusters/release"
api_dir="/data/clusters/api_release"
aiwobu_version=`ls -l ${source_dir}|tail -n +2|grep aiwobu|awk '{print $9}'`
buaini_version=`ls -l ${source_dir}|tail -n +2|grep buaini|awk '{print $9}'`
boom_version=`ls -l ${source_dir}|tail -n +2|grep boom|awk '{print $9}'`
service_version=`ls -l ${source_dir}|tail -n +2|grep service|awk '{print $9}'`
#---------------------------------------获取web版本号并存储到列表当中--------------------------------------
get_version () {
    #声明Version_list是一个列表
    declare -a Version_list
    #列表去重(备份时有用,此处没有多大用,只是生成了一个列表)
    for Version_name in ${1}
    do
         #判断元素是否在数组中
        if echo "${Version_list[@]}" | grep "${Version_name:1:3}" &>/dev/null;then
            continue
        else
        #将元素追加到数组中
            Version_list=(${Version_list[@]} ${Version_name:1:3});
        fi
    done
    echo ${Version_list[@]}
}
#-----------------------------------解压文件到指定目录&拷贝文件&修改配置文件-----------------------------------------------
unzip_package () {
    for version_number in ${Version_dict[${key}]}
    do
        cd ${source_dir}
        package_name="v${version_number}_${key}"
        unzip  -qo ${package_name}.zip -d ${package_name}
        #区分copy时的路径
        if [ ${key} == "service" ];then
                file_path="${api_dir}/PDW.SCM.API_v${version_number}"
        elif [ ${key} == "rom" ];then
                file_path="${web_dir}/v${version_number}"
        else
            file_path="${web_dir}/v${version_number}/${key}"
        fi
        \cp -r ${package_name}/* ${file_path}
        #修改webconfig
        if [ ${key} != "service" ];then 
            #过滤出老的版本号
            old_web_version=`grep key=\"Version\" ${file_path}/Web.config |awk -F '"' '{print $4}'`
            #过滤出需要更改的版本号
            change_web_version=`grep key=\"Version\" ${file_path}/Web.config |awk -F '"' '{print $4}'|awk -F '.' '{print $3}'`
            change_web_version=$((change_web_version+1))
            new_web_version=${version_number}.${change_web_version}
            #修改版本号
            sed -i "s/key=\"Version\" value=\"${old_web_version}\"/key=\"Version\" value=\"${new_web_version}\"/g"  ${file_path}/Web.config
            echo "${key}"
            echo "${old_web_version} ==> ${new_web_version}"
        fi
    done
}
#获取需要发布内容的版本号
service_version_list=`get_version "${service_version}"`
boom_version_list=`get_version "${boom_version}"`
buaini_version_list=`get_version "${buaini_version}"`
aiwobu_version_list=`get_version "${aiwobu_version}"`
#声明Version_dict是一个字典
declare -A Version_dict
Version_dict=(
    [service]="${service_version_list}"
    [boom]="${boom_version_list}"
    [buaini]="${buaini_version_list}"
    [aiwobu]="${aiwobu_version_list}"
)
#-----------------------------------------主体逻辑------------------------------------------
for key in $(echo ${!Version_dict[*]})
do
	#去除value为空的key
	if [ -n "${Version_dict[${key}]}" ];then
		if [ ${key} == "service" ];then
			unzip_package
		elif [ ${key} == "boom" ];then
			unzip_package
		elif [ ${key} == "buaini" ];then
			unzip_package
		elif [ ${key} == "aiwobu" ];then
			unzip_package	
		else
			continue
		fi
	fi
done

Guess you like

Origin blog.csdn.net/weixin_37038498/article/details/89174944