I am trying to create a script that will search for a file and if the file is there the script will end successfull, if the file is not found then the script will sleep for 60 then check for the file again. If the file is there the job will complete successfully, if it is still not there, the script will fail. Need help!!!!!!
#! /bin/ksh
if $MYHOME/jay.txt
echo "file was found"
echo "job is successful" ; then
exit 0
if $MYHOME/jay.txt ; then
sleep 60
ls -ltr $MYHOME/jay.txt
echo "file still was not found"
echo "incident will automatically be openedl"
fi
else
if $MYHOME/jay.txt
echo "file was found"
echo "job is successful" ; then
exit 0
You cannot just do an 'if <filename>'! You need to check if the file exists using 'test'. Check the man page for test and look through a couple of scripting tutorials. Go here for the tutorials.
$? holds the exit value of the previous command. usually 1 if it has failed and 0 for success. For multiple test scenarios your could use && or || to add or remove the more conditions. or you could go for if-> elif -> else -> fi statements when you have to check the condition and perform somethings when previous condition has failed. Please refer to tutorials here
#/usr/bin/ksh
COUNT=1
MAXCOUNT=10
SLEEPTIME=60
while [ ! -f ${MYHOME}/jay.txt -a ${COUNT} -le ${MAXCOUNT} ]
do
((COUNT=${COUNT}+1))
sleep ${SLEEPTIME}
done
if [ -f ${MYHOME}/jay.txt ]
then
echo file found
exit 0
else
echo file not found
exit 1
fi
This will check up to 10 times with an interval of 60 seconds if the file is present or not.
As soon as the file is present, or if the file still isn't present after 10 checks, the loop will end.