0

I am working on a script that would loop through a text file set up as such:

snap-foo    bar     20170202

This text file basically has Snapshot IDs, a tag we use to identify the instances associated, and the timestamp when the snapshots were created in AWS. I have almost got my script done but I keep getting an error when working on the last bit of it:

#!/bin/bash

THIRTY_DAYS_AGO=$(date --date '30 days ago' "+%Y%m%d")
FILE=/home/rkahil/sorted_snaps.txt

for a in $FILE
do
        SNAP_ID=`awk {'print $1'} "$FILE"`
        FORMAT_FILE=`sed -i 's/T.*//' "$FILE"`
        TIMESTAMP_SNAPS=`cat "$FILE" | awk {'print $3'}`
        if [[ "$FORMATTED_DATE" -lt "$THIRTY_DAYS_AGO" ]];
        then
                aws ec2 delete-snapshot --dry-run "$SNAP_ID" #Where the error is
        fi
done

The error I get is:

./removeOldSnapShots.sh: line 13: /usr/bin/aws: Argument list too long

Can someone tell me what would be the correct way to do this?

3
  • 1
    You set up the for loop but then don't use the loop var $a anywhere. Any particular reasons for that?
    – user218374
    Mar 22 '17 at 16:26
  • @RakeshSharma I was forgetting to use the $a variable which is most likely why I was having this issue..
    – ryekayo
    Mar 22 '17 at 16:55
  • 1
    @ryekayo that's not enough though, for a in $FILE doesn't loop through the contents of the file, it just sets a equal to FILE. Mar 22 '17 at 17:10
2

You’re not looping over the contents of the file, you’re processing all its contents in one go; so in particular, SNAP_ID ends up containing all the first column, which is apparently too much to use in a single command.

You need to process the file line by line, using something like

< "$FILE" while read SNAP_ID _ TIMESTAMP_SNAPS; do
    if ...
done

There are a number of caveats when using read, related to the impact of the contents being read (special characters, backslashes etc.), which you probably want to read up on if you want to apply this technique more generally. The shell isn't really a good file processing tool...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.