I have a unix script which searches for a specific set of gif files and finshes when all of the files are found. For example, the scripts is called as
find_gifs "andy*.gif" 5
which waits until it finds 5 gif files that match the given criteria, see code below:
#!/bin/sh
#** Ver Who When What
#** --- --- ---- ----
#** 1.00 AP 01-May-2003 Original Version
#**
#** Description
#** -----------
#** This script will search for gif files based on what is passed in the
#** command line. The script will wait until the gif is found before
#** it ends.
# By default we have not found any gifs
found=0
# Set the filename to the first command line parameter
filename=$1
target=$2
if [ -z "$target" ]
then
# By default we will only want to find 1 gif, just in case it is not passed
target=1
fi
# If no command line parameter was given, stop.
if [ -z "$filename" ]
then
echo "No filename specified"
exit 1
fi
while [ $found != $target ]
do
# Look for the gif file. Redirect standard error from the list command to
# nothing otherwise the calling web page source will contain hundreds of
# lines stating that the file can not be found
found=`ls $filename 2>/dev/null | grep -c ".gif"`
done
The problem is that I want to add a fail-safe clause in that means that the while loop will stop if the search has reached a certain amount of time, 5 minutes for example.
The only potential way forward that I can think of is to load the current date/time into a variable and each time the while loop is passed, the current time is compared to the variable.
Does anyone know of an easier way around this problem, or how I would go about hacking the date/time values for comparison.
find_gifs "andy*.gif" 5
which waits until it finds 5 gif files that match the given criteria, see code below:
#!/bin/sh
#** Ver Who When What
#** --- --- ---- ----
#** 1.00 AP 01-May-2003 Original Version
#**
#** Description
#** -----------
#** This script will search for gif files based on what is passed in the
#** command line. The script will wait until the gif is found before
#** it ends.
# By default we have not found any gifs
found=0
# Set the filename to the first command line parameter
filename=$1
target=$2
if [ -z "$target" ]
then
# By default we will only want to find 1 gif, just in case it is not passed
target=1
fi
# If no command line parameter was given, stop.
if [ -z "$filename" ]
then
echo "No filename specified"
exit 1
fi
while [ $found != $target ]
do
# Look for the gif file. Redirect standard error from the list command to
# nothing otherwise the calling web page source will contain hundreds of
# lines stating that the file can not be found
found=`ls $filename 2>/dev/null | grep -c ".gif"`
done
The problem is that I want to add a fail-safe clause in that means that the while loop will stop if the search has reached a certain amount of time, 5 minutes for example.
The only potential way forward that I can think of is to load the current date/time into a variable and each time the while loop is passed, the current time is compared to the variable.
Does anyone know of an easier way around this problem, or how I would go about hacking the date/time values for comparison.