Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a script! 1

Status
Not open for further replies.

Neofrog

IS-IT--Management
Oct 16, 2009
2
US
We have a mission critical system in which one of our services is down. Nobody here has much UNIX experience, but we were tasked with fixing the problem. It may seem simple to you guys, but anything will help. All I can provide is a description.

Please any information on syntax will be helpful.

I need the script to execute a loop that searches for a new folder every 15 minutes (sleep 900)in the home folder, runs a tar command on it, then ftp it to $REMOTE in the home folder. The only thing that's killing me is making sure it doesn't run the command on the same folder over and over.

How do I do this? Any info will help guys, I'm on severe time constraints as this is mission critical gear.

Thank you for your time. Your help is greatly appreciated.
 
Maybe put a 'flag' file in the folder once it's been processed, and have your script search for this flag file before doing anything. If it exists, do nothing, if not, do the necessary. This is a guess based on my understanding of what you want/need to do - if my understanding is faulty, maybe try to post your code so that we can have a look at the nitty gritty. HTH.

Some days are diamonds, some days are rocks - make sure most are the former.
 
Another way can be to add the processed folder to a file, and in each loop iteration make a diff from the home directory ls and this file. If there is a new folder in the home directory process it and add it to the file.
 
Here's an implementation of Ken's idea. I've typed this, but not tested it, so you may have to tweak it. You may also prefer gzip over compress, if available.

Code:
#!/usr/bin/ksh

USERNAME=username
PASSWORD=password
REMOTE=1.2.3.4
SRCDIR=/srcdir
DESTDIR=/destdir

cd $SRCDIR

while true
do
    find * -type d -prune | while read dir
    do
        if [[ ! -f $dir/.tarred ]]
        then
            touch $dir/.tarred
            tar cvf - $dir | compress -c > $dir.tar.Z
            ftp -inv $REMOTE << HERE
                user $USERNAME $PASSWORD
                bin
                put $dir.tar.Z $DESTDIR
                quit
HERE
            rm $dir.tar.Z
        fi
    done
    sleep 900
done

Annihilannic.
 
Nice Anni. The only thing I could add is that if you use "<<-" for your HERE file instead of just "<<", you don't have to left justify your final "HERE". The dash makes it remove all leading white space before using the line, so you can only use this where leading white space is meaningless. This makes it prettier to me.

Like this...
Code:
#!/usr/bin/ksh

USERNAME=username
PASSWORD=password
REMOTE=1.2.3.4
SRCDIR=/srcdir
DESTDIR=/destdir

cd $SRCDIR

while true
do
    find * -type d -prune | while read dir
    do
        if [[ ! -f $dir/.tarred ]]
        then
            touch $dir/.tarred
            tar cvf - $dir | compress -c > $dir.tar.Z
[b]            ftp -inv $REMOTE <<-HERE[/b]
                user $USERNAME $PASSWORD
                bin
                put $dir.tar.Z $DESTDIR
                quit
[b]            HERE[/b]
            rm $dir.tar.Z
        fi
    done
    sleep 900
done


 
Thanks Sam, I'm aware of the <<- functionality, and it definitely makes the code tidier, but I've had mixed results with it on some platforms (i.e. it works on the contents of the HERE document, but misses the terminater if it is also indented). I must confirm where it does and doesn't work one day... or whether I'm just under a misconception. :)

Annihilannic.
 
I just did a quick test, and it seems to work mostwhere (tested sh, bash and ksh under HP-UX, Linux, AIX and Solaris)... but there's a caveat... the indentation before both the indented code and the terminator must be tabs, not just any whitespace.

I like using tabs, but I know many people refuse to use them and have their editors set up to replace them all with spaces, so I guess that's why I've run into problems frequently.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top