In my bash script I want to use beeline to issue a "show partitions" command and then iterate over the partitions which are the output of that command. The command works fine, but I can't figure out how to iterate over the iterations. Here's my code:
#!/usr/bin/env bash
PARTITIONS=$(/usr/bin/beeline -u "$hive_llap" --showHeader=false --outputformat=csv2 -e "use my_db; show partitions my_hist_table;")
echo -e "PARTITIONS: ${PARTITIONS}"
IFS=$'\n' read -ra ptns <<< "${PARTITIONS}"
echo "ptns: ${ptns}"
for ptn in "${ptns[@]}"; do
echo "ptn: ${ptn}"
done
and here's the output:
PARTITIONS: yr=2021/mo=08
yr=2021/mo=2
yr=2021/mo=4
ptns: yr=2021/mo=08
ptn: yr=2021/mo=08
It seems that the output is going into $PARTITIONS correctly, but my attempt to put that in to the array, ptns, isn't working. I have tried using outputformat=tsv2 and \t in my IFS statement, and I get the same thing, that is, $PARTITIONS shows what are apparently newlines (not tabs, even when using tsv2).
Consider replacing:
IFS=$'\n' read -ra ptns <<< "${PARTITIONS}"
with:
read -d' ' -ra ptns <<< "${PARTITIONS}"
Which should generate:
$ typeset -p ptns
declare -a ptns=([0]="yr=2021/mo=08" [1]="yr=2021/mo=2" [2]="yr=2021/mo=4")
Another option, especially if each line from ${PARTITIONS}
may include white space:
$ mapfile -t ptns <<< "${PARTITIONS}"
$ typeset -p ptns
declare -a ptns=([0]="yr=2021/mo=08" [1]="yr=2021/mo=2" [2]="yr=2021/mo=4")