Beginner trying to write a shell script!

Dear All,

I'm trying to write a shell script that continously checks a certain folder. When a file is placed in the directory securely copies the file to another server.

I've got the secure copying working, but I don't know how to contiously check a directory for a new file and then use that file to scp.

(The files contain a date & timestamp within the file name)

Any ideas???

Thanks,

A Complete Novice

One way would be to use cron. Check the man page for crontab and cron. Set your script to run every minute of every day.

If that isn't good enough, then you would have to check out inittab - this could start your process and if it died, restart it. Then inside your script you would have to loop (along with a sleep command - to wait say 10 seconds and then check the directory for files, if there are files, move them, loop to wait)

Scripting is only putting into a file what you would do manually - the only hard part is getting the logic of things you do without realizing it ( like listing a directory and not moving or deleting certain files).

Here is one possible solution:


while true; do
  sleep 1
  if [ -f /path/to/some/file ]; then
    # Take some action
  fi
done

After reading RTM's post, you might want to up the time to 10 seconds, or 60 seconds or whatever is reasonable. 1 second is probably to tight of a loop.

You can start a script from the cron e.g. every 1 minute or you write a script with is running for ever with a sleep in it (eg. sleep 60, means a sleep for 60 sec).

eg.

#!/bin/csh
foreach FILE (`find /<your path>/ -newer LastCheck -print`)
scp /<your path>/ $FILE /<new path>/$file
...... maybe some more code ......
end

touch LastCheck

exit

To initialize this way you need to create the 'LastCheck' the first time by hand by `touch LastCheck`

every time you run this script it copies all files who are newer then the file LAstCheck to the new location

See also the find man page: