1

Being very new to bash and unix I'm once again struggling with a very basic issue, i.e. adding a string to an array. What I'm trying to achieve, is to have an array of file names matching a certain pattern. Here's my script:

declare -a TASKS=()
find /cs/srpresul/app/deploy/config/imas-config/rdx-tasks-prepared/ -name 
"IMAS-Loaders-*.json"|while read fname; do
   FILE_NAME=$(echo $fname | cut -d'/' -f11)
   echo "$FILE_NAME"
   TASKS+=("$FILE_NAME")
done
TASKS+=('foo' 'bar')
for TASK in "${TASKS[@]}"
do
   echo "$TASK"
   echo "Starting back population for type $TASK between $1 and $2"
done

So the result is as follows: the FILE_NAME variable holds proper value, as it is printed correctly, but the TASKS array does not contain them. Only 'foo' and 'bar' values are present in the array. Any ideas what am I doing wrong?

1 Answer 1

3

You are using a pipe so you are appending to TASKS in a subshell that is why it does not work. You can use the following:

declare -a TASKS=()
for fname in $(find /cs/srpresul/app/deploy/config/imas-config/rdx-tasks-prepared/ -name 
"IMAS-Loaders-*.json"); do
   FILE_NAME=$(echo $fname | cut -d'/' -f11)
   echo "$FILE_NAME"
   TASKS+=("$FILE_NAME")
done
TASKS+=('foo' 'bar')
for TASK in "${TASKS[@]}"
do
   echo "$TASK"
   echo "Starting back population for type $TASK between $1 and $2"
done
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.