1

I have a problem building a "dynamic" array.

First of all I create an array to generate a list of names of files:

declare -a pgidarr=`run "select partition_id from ETL.PARTITION_GROUP_MEMBER where partition_group_id=${PGID}"`
for i in ${pgidarr[@]}
do
ARRLOOP=$i
PAID=`run "select LPAD('${ARRLOOP}',2,'0')"` #LPAD the partition ID
FILENAME=ABCD_${PAID}_000000.txt

Now in the same loop I want to create a NEW array

trigarrat=("${trigarrat[@]}" $FILENAME)

But when I run it doens't replace $FILENAME

On Google I can't find much about arrays and variables, anyone could please help me? ;) Thanks! Alex

bash --version GNU bash, version 3.1.17(1)-release

2
  • You could probably use PAID=$(printf "%.2d" $i) to avoid going to the DB just for number formatting. Commented Mar 27, 2013 at 18:10
  • You're not initializing pgidarr properly; it's a coincidence that the only element of the array is split into words after you expand the array without quoting it. declare -a pgidarr=( $( run ... ) ), then for i in "${pgidarr[@]}" Commented Mar 27, 2013 at 18:19

1 Answer 1

3

Try using += to append elements to trigarrat:

declare -a pgidarr=`run "select partition_id from ETL.PARTITION_GROUP_MEMBER where partition_group_id=${PGID}"`
trigarrat=()
for i in ${pgidarr[@]}
do
    ARRLOOP=$i
    PAID=`run "select LPAD('${ARRLOOP}',2,'0')"` #LPAD the partition ID 
    FILENAME=ABCD_${PAID}_000000.txt
    trigarrat+=($FILENAME)
done

Reference: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameters

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.