if, fi, then, and else commands

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
:confused:

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.

ok, thanks.

It's just a matter of checking for the existence of the file and checking the status of that test.

if [ -f $MYHOME/jay.txt ]
then
xxxxxxxx
fi

if [ $? != 0 ]
then
error message
fi

Thanks, mheinen.

Alternatively you could try
[[ -f $MYHOME/jay.txt ]] && xxxxxx || echo "error"

hi mheinen

what does this do..check the status of the test..

if [ $? != 0 ]
then
error message 
fi

What about multiple test scenarios??

Thanks
Sumeet

$? 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

This is working for me. How could I put a "sleep xx" in the script so it would wait for the file before the job ended in error?

if [ -f $MYHOME/jay.txt ]
then
echo file found
exit 0
fi
if [ $? != 1 ]
then
echo file not found
exit 1
fi

#/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.

please use code tags

Thanks, sb008