Reputation: 55
I have an array that contains the UTC creation times for a few amazon ec2 volume snapshots that are in this format: 2013-08-09T14:20:47.000Z
I'm trying to figure out a way to compare the elements of the array to each other to determine which one is the oldest snapshot and delete it in Bash 4
The current code that I have right now:
#creates a count of all the snapshot volume-id's and deletes oldest snapshot if
#there are more than 5 snapshots of that volume
declare -A vol_id_count=( ["id_count"]="${snapshot_vols[@]}" )
check_num=5
for e in ${vol_id_count[@]}
do
if (( ++vol_id_count[$e] > $check_num ))
then
echo "first nested if works"
#compare UTC times to find oldest snapshot
#snapshot_time=${snapshot_times[0]}
#for f in ${snapshot_times[@]}
#do
# time= date --date="$snapshot_time" +%s
# snapshot_check=${snapshot_times[$f]}
# echo "check: "$snapshot_check
# check= date --date="$snapshot_check" +%s
# if [[ "$snapshot_time" -gt "$snapshot_check" ]]
# then
# snapshot_time=$snapsnapshot_check
# echo "time: "$snapshot_time
# fi
#done
#snapshot_index=${snapshot_times[$snapshot_time]}
#aws ec2 delete-snapshot --snapshot-id "${snapshot_ids[$snapshot_index]}"
fi
done
I have the first for loop and if statement working to check if there are more than 5 snapshots of a certain volume but I'm scratching my head trying to figure out how to compare the UTC strings. Would a second associative array do the trick I wonder?
Upvotes: 1
Views: 979
Reputation: 2346
If you already have an array, no need to use a loop to print the items. A single echo will work (but only if there are no spaces in the filenames):
oldest=`echo "${snapshot_times[@]}" | tr ' ' '\12' | sort -r | tail -1)`
If those "timestamps" are coming from the filenames themselves, just use ls
in a pipe:
snapshot_dir=/some/path/to/snapshots
count=`ls -1 $snapshot_dir/ | wc -l` # get count of snapshots
# expunge the oldest snapshot if there are more than 5
if (( count > 5 )); then
oldest_name=`(cd $snapshot_dir ; ls -1 ) | sort | head -1`
oldest_file="$snapshot_dir/$oldest_name"
[[ -f "$oldest_file" ]] && {
echo "Purging oldest snapshot: $oldest_file"
rm -f $oldest_file
}
fi
Upvotes: 0
Reputation: 8021
Here's how I would do it: with sort
.
dates=(
2013-08-09T14:20:47.000Z
2013-08-09T14:31:47.000Z
)
latest=$(for date in ${dates[@]}
do
echo $date
done | sort | tail -n 1)
echo $latest # outputs "2013-08-09T14:31:47.000Z"
This is obviously just a sample script, modify as needed - it could easily be turned into a function like:
function latest() {
local dates="$@"
for date in ${dates[@]}
do
echo $date
done | sort | tail -n 1
}
latest ${dates[@]} # outputs "2013-08-09T14:31:47.000Z"
Upvotes: 2