将列表转换为存储在变量中的值的数组



我有一个配置文件。第一列是作业名称,其余列是与该作业关联的表。现在我正在读第一排。它将在中检查作业名称和与其关联的表。现在,我想循环浏览这些表,并创建一个HQL查询。我的代码的问题是,表以列表的形式存储在一个变量中,当我执行unset命令时,它会抛出错误。有人能帮我哪里出错吗。下面是配置文件、代码和所需的输出。

job1,tbl1,tbl2,tbl3
job2.tbl4,tbl5,tbl6
job3,tbl7,tbl8,tbl9

下面是我正在尝试的代码

#!/bin/bash
while read line; do
job_name=$(echo "$line"|awk 'BEGIN{FS=","}{print $1}')
echo "JobName is $job_name"
tablestring=${line#"$job_name"}
for table in ${tablestring//,/ }; do
echo "$table"
done
hql="select $table as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.$table "
unset tablestring[0]
for tbl in "${tablestring[@]}"
do
hql+=$'n'
hql+="union all select $tbl as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.$tbl"
hql+=";"
echo "$hql" >query.hql
done
done < a.configuration

以下是我希望在作业1的第一次迭代中以及在作业2的第二次迭代中实现的所需输出,依此类推。

select tbl1 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl1
union all select tbl2 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl2
union all select tbl3 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl3;

所以我会用它作为配置文件.cfg

job[1]="jobname1 tbl1 tbl2 tbl3"
job[2]="jobname2 tbl4 tbl5 tbl6 tbl7"
job[3]="jobname3 tbl8 tbl9"

这个脚本

#!/bin/bash
TimeStamp=$(date +"%Y%m%d%H%M%S")
#Sourcing the configuration file. Here in the same directory as this script but you can provide the full path
. ./a.cfg
for i in ${!job[@]}; do
#Creating the array jobtable. The job name is always index 0
jobtable=( ${job[$i]} )
#Using a unique name for each file
filename=${TimeStamp}_${jobtable[0]}.hql
echo "Processing ${jobtable[0]} using"
for ((j=1;j<${#jobtable[@]};j++)); do
echo "    ${jobtable[$j]}"
#Creating the hql file
if [[ $j -eq 1 ]]; then
echo "select ${jobtable[$j]} as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.${jobtable[$j]} " >> $filename
elif [[ $j -lt $((${#jobtable[@]}-1)) ]]; then
echo " union all select ${jobtable[$j]} as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.${jobtable[$j]}" >> $filename
else
echo " union all select ${jobtable[$j]} as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.${jobtable[$j]};" >> $filename
fi
done
#You can use the newly created hql file here using $filename. I would not delete the file though as it might be usefull to
#troubleshoot if something is not right.
done

它确实产生了像这样的hql文件一个

select tbl1 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl1
union all select tbl2 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl2
union all select tbl3 as table_name, max(cycle_Date) as AS_OF_dATE FROM DB1.tbl3;

编辑:纠正打字错误的小编辑。

相关内容

  • 没有找到相关文章

最新更新