Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Conditional execution in a script with FIND
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
fincoop
Tux's lil' helper
Tux's lil' helper


Joined: 02 Feb 2004
Posts: 143

PostPosted: Mon Nov 07, 2011 12:14 am    Post subject: [SOLVED] Conditional execution in a script with FIND Reply with quote

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
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10589
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Nov 07, 2011 12:52 am    Post subject: Reply with quote

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
:wink:

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
fincoop
Tux's lil' helper
Tux's lil' helper


Joined: 02 Feb 2004
Posts: 143

PostPosted: Mon Nov 07, 2011 1:16 am    Post subject: [solved] Reply with quote

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
:wink:

- John


You nailed it. Thanks John!
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21635

PostPosted: Mon Nov 07, 2011 2:00 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum