while or until ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while or until ?
# 1  
Old 07-31-2012
while or until ?

Hi, I need to write a script for fetching 2 files which come(thru FTP) to server1 at a particular location (/u/home/folder1). Now the condition is, the files are transmitted from another server (server2) between 3pm to 4pm (server time). The script should check if both files have come to the location (/u/home/folder1). If one file has come (thru FTP) and if the other has not yet arrived, then the script must wait for some time and then check, if the file has arrived, then the script should end by emailing me that both files have arrived. I've built the code for FTP to get the files. The problem is what could be done to check the files for that long period of time? And the script should run in the background.
Please help in this regard.
# 2  
Old 07-31-2012
I'd just check for both files until the deadline runs out, and extend the deadline when the first file arrives.

Whether you use a while or an until is a simple matter of preference, you can make the logic work with both.

Code:
DEADLINE=$((SECONDS+1200)) # Ten minutes/1200s from when script starts

LAST1=0

# Wait until both files have arrived
while [ ! -f /path/to/file1 ] || [ ! -f /path/to/file2 ]
do
        # If the first file has arrived and we haven't already extended the
        # deadline, extend it.
        if [ -f /path/to/file1 ] && [ "$LAST1" -eq 0 ]
        then
                LAST1=1
                DEADLINE=$((DEADLINE+1200)) # Extend deadline
        fi

        if [ "$SECONDS" -gt "$DEADLINE" ]
        then
                echo "Deadline expired"
                exit 1
        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question