0

I would like to store a variable ($random) from the loop statement - currently only the last statement from the loop is printed. Please note that I would like to use this variables outside of the loop.

#!/usr/bin/env bash

for backup in $(s3cmd ls s3://bucket/ | awk '{ print $2 }')
do
latest=$(s3cmd ls $backup | awk '{ print $2 }'  | sed -n '$p')
random=$(s3cmd ls $latest | shuf | awk '{ print $4 }' | sed -n '1p')
        s3cmd get $random $data_dir >/dev/null 2>&1
done

echo "$random

1 Answer 1

1

It's not entirely clear what you are asking, but I think you are saying you want to keep a record of each value $random receives during execution of the loop.

declare -a randoms

for backup in $(s3cmd ls s3://bucket/ | awk '{ print $2 }')
do
latest=$(s3cmd ls $backup | awk '{ print $2 }'  | sed -n '$p')
random=$(s3cmd ls $latest | shuf | awk '{ print $4 }' | sed -n '1p')
        s3cmd get $random $data_dir >/dev/null 2>&1
randoms+=( $random )
done

echo ${randoms[@]}
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.