[SOLVED] Older than $(date +"%m%d%Y" --date="14 days ago") script help
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Older than $(date +"%m%d%Y" --date="14 days ago") script help
Hello:
I have some homegrown AWS scripts that we use to manage AMIs and Snapshots.
Right now, the requirement is anything older than 14 days - de-register AMI and then delete associated snapshots. That part is all good, however, I need help with this function please
As you can see, I have a "$DEL_DATE" and I'd love to see grep "$DEL_DATE" or some other calculated field there, BUT I do not know how to perform the calculation on > "$DEL_DATE"
I am editing the grep by hand to accommodate the requirement presently.
and here's the entire script for grins and giggles.
Code:
#!/bin/bash
# AMN AWS Creds:
# Zone: WEST
# 14 days and older entity removal.
# DEL_DATE=$(date +"%m%d%Y" --date="14 days ago")
# DEL_DATE2=$(date +"%m%d%Y" --date="today")
export EC2_URL=https://ec2.us-west-1.amazonaws.com
export AWS_ACCESS_KEY=
export AWS_SECRET_KEY=
export EC2_HOME=/home/c9admin/ec2/ec2-api-tools/ec2-api-tools-1.6.6.3
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=/usr
IMAGES=$(ec2-describe-images | awk '{print $2 " "$3}' | grep 031[1-8]2013 | awk '{print $1}')
SNAPSHOTS=$(ec2-describe-images $IMAGES | grep snap | awk '{print $4}')
REPORT=/home/c9admin/amn_DELETED_DEREGd.lst
echo "" > "$REPORT"
DE_REGISTER()
{
for i in `echo "$IMAGES"`
do ec2-deregister $i >> "$REPORT"
done
}
DEL_SNAPSHOTS()
{
for i in `echo "$SNAPSHOTS"`
do ec2-delete-snapshot $i >> "$REPORT"
done
}
MAIL_RESULTS()
{
mail -s "AMN Image and Snapshot Report" mail@domain.com < "$REPORT"
}
#safety net
if [ "$IMAGES" > 0 ]; then
DE_REGISTER
DEL_SNAPSHOTS
MAIL_RESULTS
else
echo "No IMAGES to process" >> "$REPORT"
MAIL_RESULTS
exit
fi
#EOF
Any Questions? I'd be happy to answer them!
Thank you all for your time.
Last edited by Habitual; 04-03-2013 at 10:12 AM.
Reason: terminated string '"14 days ago'
Hello:
I have some homegrown AWS scripts that we use to manage AMIs and Snapshots. Right now, the requirement is anything older than 14 days - de-register AMI and then delete associated snapshots. That part is all good, however, I need help with this function please
As you can see, I have a "$DEL_DATE" and I'd love to see grep "$DEL_DATE" or some other calculated field there, BUT I do not know how to perform the calculation on > "$DEL_DATE"
I am editing the grep by hand to accommodate the requirement presently.
Any Questions? I'd be happy to answer them!
What is the output of the "ec2-describe-images" command? And you may have thought of this already, so forgive a potentially stupid question, but since you have the $DEL_DATE, and you want anything older (i.e. LESS THAN) that number string (today, the DEL_DATE command returns 03202013), could you perhaps use awk or a different grep?
Code:
awk '$1 < $DEL_DATE'
or
Code:
grep -E -i "$DEL_DATE"
or remove the date-check from the ec2-describe images command. Clunky, but may work:
Code:
IMAGES=`ec2-describe-images`
SNAPSHOTS=`ec2-describe-images $IMAGES`
DE_REGISTER()
{
for i in `echo "$IMAGES"`
# Statement here to get the date string into a number
IMGDATE=$(ec2-describe-images | awk '{print $2 " "$3}' | grep 031[1-8]2013 | awk '{print $1}')
# Compare the numbers, since you have $DEL_DATE already
if [ $IMGDATE -lt $DEL_DATE ]
do ec2-deregister $i >> "$REPORT"
fi
done
}
DEL_SNAPSHOTS()
{
for i in `echo "$SNAPSHOTS"`
# Same logic here
SNAPDATE=$(ec2-describe-images $ | grep snap | awk '{print $4}')
# Compare the numbers, since you have $DEL_DATE already
if [ $SNAPDATE -lt $DEL_DATE ]
do ec2-delete-snapshot $i >> "$REPORT"
fi
done
}
Completely untested, and probably crap. Bash isn't a strong suit with me, and I don't know what the ec2 command is outputting to work with.
What is ec2-describe-images, a function or script? And what does it's output look like? I'm guessing it reads image metadata or something?
The usual way to get 'older than' files is to use find, but then that usually searches for the last modified time of the files. If you're reading metadata that would be different.
At the least, however, I suggest you take some time to learn how to properly use tools like awk. Chains of multiple cut|grep|sed|awk commands can almost always be reduced to a single instance of sed or awk.
(Examples from the OP)
Code:
# IMAGES=$(ec2-describe-images | awk '{print $2 " "$3}' | grep 031[1-8]2013 | awk '{print $1}')
IMAGES=$( ec2-describe-images | awk '/031[1-8]2013/ { print $2 }' )
#I believe this is equivalent, it's hard to tell without seeing the input.
# SNAPSHOTS=$(ec2-describe-images $IMAGES | grep snap | awk '{print $4}')
SNAPSHOTS=$( ec2-describe-images $IMAGES | awk '/snap/ {print $4}' )
One more thing with the above, though. I assume that the IMAGES line results in the variable containing multiple entries. But it's almost never a good idea to store lists of related things in a single variable. As long as you're using bash or another advanced shell, you should use arrays instead. That's what they're designed for.
if [ "$IMAGES" > 0 ]; then
if [ -n "$IMAGES" ]; then
When using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.
In addition, when inside square bracket tests, the symbols '>,<,=' and the like are string comparisons. You have to use '-gt,-lt,-eq' if you want to do numeric comparisons. Another reason why "((..))" is preferable here.
Code:
if (( IMAGES > 0 )); then
if [[ -n $IMAGES ]]; then
Finally, a bit of a meta-point. I see your date strings are in mmddyyyy format. I recently learned that the recommended form is to use the ISO-8601 international standard format of YYYY-MM-DD whenever possible.
If you can update your scripts and filenames to use the standard, it would let the shell to do natural sorting, and would probably make it easier to use globbing or other simple comparative techniques for getting the names you want.
If you don't want to go that way, you should try converting your date strings to epoch seconds (date +%s), and again you'll be able to use simple arithmetic to compare dates.
I understand that I should use ISO-8601 format in my creation script. and
Code:
if [[ -n $IMAGES ]]; then
is preferred over:
Code:
if [ -n $IMAGES ]; then
wrt:
Quote:
I recently learned that the recommended form is to use the ISO-8601 international standard format of YYYY-MM-DD whenever possible.
If you can update your scripts and filenames to use the standard, it would let the shell to do natural sorting, and would probably make it easier to use globbing or other simple comparative techniques for getting the names you want.
Here's an example of what you can do if your filenames are in standard date order:
Create files for every day in march and april in YYYYmmdd format.
(Yes, it creates a march 31 too, but it's just an example.)
Code:
touch file_2013{03..04}{01..31}.txt
Get the date for 14 days ago in the same format (currently March 24th)
Code:
compdate=$( date -d '-14 days' '+%Y%m%d' )
Loop through the directory and print out every file that has a name earlier than the comparison date. Use a simple parameter substitution to remove all non-digit characters from the name before comparing.
Code:
for fname in *; do
if (( ${fname//[^0-9]} < compdate )); then
echo "$fname"
fi
done
Notice how you can use simple gt/lt comparisons on the dates when they're in the non-hyphenated format. (Actually, this would still work anyway, as the hyphens would be removed from the name as well during comparison).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.