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.

esahmo
  • 126
  • 1
  • 2
  • 9

1 Answers1

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.

konsolebox
  • 72,135
  • 12
  • 99
  • 105