2

This is the script which I am trying to run

bRules=(`aws s3api get-bucket-lifecycle-configuration --bucket x |  jq '.Rules[]'.ID`)
echo "*** Total number of LifecycleRules ****  ${#bRules[@]}"
for rule in "${bRules[@]}"
do
    echo "Bucket: x, Rule: $rule"
done

Actual output

*** Total number of LifecycleRules ****  6
 Bucket: x, Rule: This
 Bucket: x, Rule: is
 Bucket: x, Rule: Rule-1
 Bucket: x, Rule: This
 Bucket: x, Rule: is
 Bucket: x, Rule: Rule-2

Expected output:

*** Total number of LifecycleRules ****  2
Bucket: x, Rule: This is Rule-1
Bucket: x, Rule: This is Rule-2

What needs to be change in my code snippet to get the desired output? I'm kinda of lost to fix this issue.

0

1 Answer 1

1

Don't rely on command substitution and word splitting. Use readarray (or mapfile if you prefer) and process substitution instead.

Your loop can also be simplified to a single printf.

readarray -t bRules < <(aws s3api get-bucket-lifecycle-configuration --bucket x | jq '.Rules[]'.ID)
echo "*** Total number of LifecycleRules ****  ${#bRules[@]}"
printf 'Bucket: x, Rule: %s\n' "${bRules[@]}"

Read the Bash Manual.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.