简化bash脚本生成的where子句

gab6jxml  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(344)

我有下面的变量 bash ```
source_FROM_batch_date='2020-06-06'
source_to_batch_date='2020-06-07'
min_batch_date_seq_num=2
max_batch_date_seq_num=3

我的数据加载成批运行 `1` 至 `4` 以及 `4` 最大批量。
我想生成一个 `where` 动态地基于上面的变量
batch\ date和batch\ seq\ num是我将过滤数据的列 `conditions` ```
1) read all the data where batch_date = '$source_FROM_batch_date' and batch_seq_num >= 'min_batch_date_seq_num'
2) read all the data where batch_date = '$source_to_batch_date' and batch_seq_num <= 'max_batch_date_seq_num'
3) read all the data that occurs between $source_FROM_batch_date and $source_to_batch_date

我做了如下的事情。


# !/bin/bash

run_project_tmp_dir='/home/$USER'

source_FROM_batch_date='2020-06-06'
source_to_batch_date='2020-06-07'
min_batch_date_seq_num=2
max_batch_date_seq_num=3

export min_batch_date=${source_FROM_batch_date}
export max_batch_date=${source_to_batch_date}
export min_batch_date_seq_num=${min_batch_date_seq_num}
export max_batch_date_seq_num=${max_batch_date_seq_num}

#################### Generate batch filter#######################

startdate=${min_batch_date}
enddate=${max_batch_date}
d=
n=0
loop_cnt=0
start_seq=${min_batch_date_seq_num}
end_seq=${max_batch_date_seq_num}
max_seq=4
max_seq_num=$((max_seq + 1))

batch_filter_file=${run_project_tmp_dir}/batch_filter_file.txt

if [ -f ${batch_filter_file} ]; then
    rm -f ${batch_filter_file}
fi

until [ "$d" = "$enddate" ]

do
    d=$(date -d "$startdate + $n days" +%Y-%m-%d)

    ## Case when only one batch to process
    if [[ $d = ${enddate} && ${start_seq} = "${end_seq}" && ${loop_cnt} = 0 ]];then
        echo "batch_date='$d' AND batch_seq_num='$start_seq'" >> ${batch_filter_file}
    fi

    ## Case when multiple batches on same batch date
    if [[ $d = ${enddate} && ${start_seq} -ne ${end_seq} && ${loop_cnt} = 0 ]];then
        until [ "$start_seq" = $((end_seq + 1)) ]
    do

    echo "(batch_date='$d' AND batch_seq_num='$start_seq') OR " >> ${batch_filter_file}

    ((start_seq++))

    done
    fi

    if [[ $d != "${enddate}" ]];then
        until [ "$start_seq" = "$max_seq_num" ]
    do

    echo "(batch_date='$d' AND batch_seq_num='$start_seq') OR " >> ${batch_filter_file}

    ((start_seq++))

    done
    fi

    if [[ $d = "${enddate}" && ${loop_cnt} != 0 ]];then
        until [ "$start_seq" = $((end_seq + 1)) ]
    do

    echo "(batch_date='$d' AND batch_seq_num='$start_seq') OR " >> ${batch_filter_file}

    ((start_seq++))

    done
    fi

    ((n++))
    ((loop_cnt++))
    start_seq=1

done

if [ -f ${batch_filter_file} ]; then
    sed -i '$s/OR $//' ${batch_filter_file}
    sed -i '1i (' ${batch_filter_file}
    echo ")" >> ${batch_filter_file}
fi
``` `output` ```
(
(batch_date='2020-06-06' AND batch_seq_num='2') OR 
(batch_date='2020-06-06' AND batch_seq_num='3') OR 
(batch_date='2020-06-06' AND batch_seq_num='4') OR 
(batch_date='2020-06-07' AND batch_seq_num='1') OR 
(batch_date='2020-06-07' AND batch_seq_num='2') OR 
(batch_date='2020-06-07' AND batch_seq_num='3') 
)
``` `required output` ```
(
(batch_date='2020-06-06' AND batch_seq_num in ('2', '3', '4') OR 
(batch_date='2020-06-07' AND batch_seq_num in ('1', '2', '3') 
)

我怎样才能达到我想要的

3lxsmp7m

3lxsmp7m1#

对于多批次:

wclause="(batch_date=... and batch_seq_num in"

然后在每个序列的循环内:

wclause="${wclause}(${start_seq}"    # for first seq
wclause="${wclause},${start_seq}"    # rest of seq's

退出循环后:

wclause="${wclause}))"
echo "${wclause}"

相关问题