View previous topic :: View next topic |
Author |
Message |
fincoop Tux's lil' helper
Joined: 02 Feb 2004 Posts: 143
|
Posted: Mon Nov 07, 2011 12:14 am Post subject: [SOLVED] Conditional execution in a script with FIND |
|
|
Problem: I have a cron job that runs every night to back up a number of directories that contain only symbolic links to other files. In the morning (every morning) it generates an e-mail that says pretty much the same thing. I wanted to add some logic to the backup script so that it only runs when a new symbolic link is added to one of the target folders. I have found that FIND can determine if there are new links, but where I'm stuck is if find comes up empty I want the script to abort.
Code: | #!/bin/bash
#
# Backup Script
IFS=$'\n'
TARGET="/share/backup/medialinks_$(date +%Y%m%d).tar"
EXCL="/etc/backup.excl"
# LOGIC TO FIND NEW LINKS
find /share/media -type l -mtime -1 \;
if [ -e "$TARGET" ] || [ -e "$TARGET.bz2" ]; then
echo "File exists"
exit
fi
tar cf $TARGET -X $EXCL "/share/media/Movies By Genre"
tar rf $TARGET -X $EXCL "/share/media/Movies By Rating"
bzip2 $TARGET
# Cleanup
find /share/backup/medialinks* -mtime +7 -delete |
TIA.
Last edited by fincoop on Mon Nov 07, 2011 1:17 am; edited 1 time in total |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Nov 07, 2011 12:52 am Post subject: |
|
|
Code: | if [ `find /share/media -type l -mtime -1 | wc -l` -gt 0 ] ; then
# Do something.
echo "Found some."
else
# Abort here.
exit 0
fi |
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
fincoop Tux's lil' helper
Joined: 02 Feb 2004 Posts: 143
|
Posted: Mon Nov 07, 2011 1:16 am Post subject: [solved] |
|
|
John R. Graham wrote: | Code: | if [ `find /share/media -type l -mtime -1 | wc -l` -gt 0 ] ; then
# Do something.
echo "Found some."
else
# Abort here.
exit 0
fi |
- John |
You nailed it. Thanks John! |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21635
|
Posted: Mon Nov 07, 2011 2:00 am Post subject: |
|
|
As a minor optimization, you could do: Code: | if [[ "$(find /share/media -type l -mtime -1 | head -n1)" != "" ]] | This has the advantage that as soon as any match is printed, head will terminate and cause the find to die as well. The method posted by John works, but will allow the find to run to completion even after it could have decided that work exists. |
|
Back to top |
|
|
|