Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code

for F in $(find $DIR -name "SALESORDER*.dat")
do
IN=${F##//}
echo $IN
Execute some shell script
done
for F in $(find $DIR/in -name "PURCHASEORDER
.dat")
do
IN=${F##/*/}
echo $IN
Execute some shell script.
done

Can any one of you please guide me how not to process any file if SALESORDER file is not there.

Thanks

for F in $(find $DIR -name "SALESORDER*.dat")
do
F##//
if [$? -eq 0]
then
IN=${F##/
/}
echo $IN
Execute some shell script

echo "now Purchase order process can start"

for F in $(find $DIR/in -name "PURCHASEORDER*.dat")
do
IN=${F##/*/}
echo $IN
Execute some shell script.
done

else
echo "Salesorder not exist "
fi
done

Let me know if this is working

Thanks
Sha

Thanks Sha

thats working but i want to get non-zero return code if the SALESORDER file is not there.

Thanks

Hmm, are the purchaseorder salesorder files postfixed with a number?
Is there some 1 to 1 correlation?
If so Then cut out the number from the salesorder file name. Build the purchase order file string and use test to determine the existence of the file. If it doesn't exist set a variable and exit.

HTH

use the "test" command with the "-f" option. It tests for the existence of a (regular) file. You could even refine your test with the "-r" option, which tests for a regular file which has read access:

if [ -f /path/to/file ] ; then
     print - "file /path/to/file exists and is a regular file"
else
     print "file /path/to/file does not exist or is no regular file"
fi

if [ -r /path/to/file ] ; then
     print - "file /path/to/file exists and is a readable regular file"
else
     print "file /path/to/file does not exist or is not readable or no regular file"
fi

Employing the logic you use it would be possible to set a flag:

#! /bin/ksh
typeset -i lFoundSO=0
typeset    fDir="/some/where"
typeset    fFileBuffer=""

ls $fDir/SALESORDER*.dat | while read fFileBuffer ; do
     lFoundSO=1
     do_some_processing $fFileBuffer
done

if [ $lFoundSO eq 1 ] ; then
     ls $fDir/PURCHASEORDER*.dat | while read fFileBuffer ; do
          do_some_other_processing $fFileBuffer
     done
fi

if [ $lFoundSO eq 0 ] ; then
     print -u2 "Error: no SALESORDER-file found"
     exit 1
fi

exit 0

Notice that i have not used a for-loop but a while-loop instead. The reason is that the commandline has a limited length (4096 characters per POSIX 1003). As the wildcards get expanded when processing the commandline it can well be that many matching files would make a string too long for the shell to digest. You would get a "line too long-error". Therefore the while-loop and a pipeline is more safe than the for-loop.

I hope this helps.

bakunin