Last active
December 18, 2022 19:04
-
-
Save br3ndonland/102d2e01ce31c0aa24323d01ec76e79f to your computer and use it in GitHub Desktop.
AWS S3 bucket names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# get a list of S3 bucket names | |
# ----------------------------- | |
# aws returns the bucket creation date and name: | |
# 2022-02-22 22:22:22 bucket-name | |
# additional logic is required to produce an array of names. | |
# pure bash | |
# https://github.com/dylanaraps/pure-bash-bible/issues/113 | |
# "read returns 1 when everything is read, which is convenient when used in a while loop" | |
BUCKETS="$(aws s3 ls)" | |
IFS=$'\n' read -d "" -ra BUCKETS_ARRAY <<<"$BUCKETS" || true | |
for i in "${!BUCKETS_ARRAY[@]}"; do | |
BUCKETS_ARRAY[i]="${BUCKETS_ARRAY[i]##* }" | |
done | |
echo ${#BUCKETS[@]} # 1 | |
echo ${#BUCKETS_ARRAY[@]} # number of buckets | |
# pure bash 4+ using mapfile | |
# https://github.com/dylanaraps/pure-bash-bible/issues/38 | |
BUCKETS="$(aws s3 ls)" | |
mapfile BUCKETS_ARRAY <<<"$BUCKETS" | |
for i in "${!BUCKETS_ARRAY[@]}"; do | |
BUCKETS_ARRAY[i]="${BUCKETS_ARRAY[i]##* }" | |
done | |
echo ${#BUCKETS[@]} # 1 | |
echo ${#BUCKETS_ARRAY[@]} # number of buckets | |
# bash 4+ using mapfile with cut | |
BUCKETS="$(aws s3 ls | cut -d ' ' -f 3)" | |
mapfile BUCKETS_ARRAY <<<"$BUCKETS" | |
echo ${#BUCKETS[@]} # 1 | |
echo ${#BUCKETS_ARRAY[@]} # number of buckets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment