Friday, 9 August 2013

Comparing UTC date/time strings in Bash

Comparing UTC date/time strings in Bash

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?

No comments:

Post a Comment