shell中map的用法

前端之家收集整理的这篇文章主要介绍了shell中map的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

##实例代码

#!/bin/bash

cat ./switchsql.txt | while read line
do
	tmp_partition_name=`echo $line | awk -F ' ' '{print $6}'`
	if [[ "$tmp_partition_name" = "" ]]; then
		continue
	fi
	
	start_flag=${tmp_partition_name:0:3}
	date_flag=${tmp_partition_name:((${#tmp_partition_name} - 6))}
	echo "${start_flag}${date_flag}" >> ./table_partition_date.txt
done
sort ./table_partition_date.txt | uniq > ./tpd_map.txt
rm ./table_partition_date.txt

declare -a partition_value_map=()
OUTFILE=`cat ./tpd_map.txt`
while read line
do
	#业务数据后台查询,这里只模拟
	part_flag="2"
	partition_value="170724"
	if [[ "$line" =~ "PD" ]]; then
		partition_value="170718"
		part_flag="1"
	fi

	if [[ "$line" =~ "PM" ]]; then
		partition_value="170801"
		part_flag="3"
	fi

	if [[ "$line" =~ "PY" ]]; then
		partition_value="180717"
		part_flag="4"
	fi
	
	date_flag=${line:((${#line} - 6))}
	partition_value_map["${part_flag}${date_flag}"]="${partition_value}"
done <<EOF
$OUTFILE
EOF

get_partition_high_value(){
	partition_name=$1
	part_flag="2"
	if [[ "$partition_name" =~ "PD" ]]; then
		part_flag="1"
	fi
	if [[ "$partition_name" =~ "PM" ]]; then
		part_flag="3"
	fi
	if [[ "$partition_name" =~ "PY" ]]; then
		part_flag="4"
	fi

	date_flag=${partition_name:((${#partition_name} - 6))}
	match_str="${part_flag}${date_flag}"
	high_value=""
	for key in ${!partition_value_map[@]}
	do
		if [ "$match_str" = "$key" ] ; then
			high_value="${partition_value_map[$key]}"
			break;
		fi
	done
	echo $high_value
}

high_value=`get_partition_high_value "PD_IND_H_1234_170717"`
echo $high_value
high_value=`get_partition_high_value "PW_IND_H_1234_170717"`
echo $high_value
high_value=`get_partition_high_value "PM_IND_H_1234_170701"`
echo $high_value

##组织数据

alter table PM4H_DB.IND_M15_44 exchange partition PD_IND_M15_44_170717 with table PM4H_DB.PW_IND_M15_44_160704;
alter table PM4H_DB.IND_2D_33_1 exchange partition PW_IND_2D_33_1_170717 with table PM4H_DB.PW_IND_2D_33_1_160704;
alter table PM4H_DB.IND_2H_62_12 exchange partition PM_IND_2H_62_12_170701 with table PM4H_DB.PW_IND_2H_62_12_160704;
exit;

##运行结果

pm22:/netwatcher/dbbackup> sh map.sh 
170718
170724
170801
原文链接:https://www.f2er.com/bash/391820.html

猜你在找的Bash相关文章