Check for File Existence


 
Thread Tools Search this Thread
Operating Systems AIX Check for File Existence
# 1  
Old 08-08-2008
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
# 2  
Old 08-08-2008
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
# 3  
Old 08-08-2008
Thanks Sha

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

Thanks
# 4  
Old 08-08-2008
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
# 5  
Old 08-08-2008
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:

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

Code:
#! /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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File existence check

hi i wanted to check if the file exist or not(multiple files) DIRE=/home/V478 if ; then echo "file present" else echo "file not present" fi But i am getting the error as : [: unexpected operator/operand (3 Replies)
Discussion started by: ATWC
3 Replies

2. UNIX for Dummies Questions & Answers

To check for existence of a file

I need to check for the existence of a file *.log in a specific directory using a perl script. Presently am not in that particular directory. So i am using chdir ("/path/to/my/file) And then i am using the -e in an if statement to check if it exists. if (-e $File) {......} $File contains the... (1 Reply)
Discussion started by: manutd
1 Replies

3. Shell Programming and Scripting

command to check existence of file name which has regex

Hi All. Pls help me with the command to check existence of files (I'll mention name of the file as regex) and proceed with my further processing if atleast one of them exists in detail, I've a dir /tmp/TURP, which may or may not have files named with "exter*.txt" I need to check and... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

4. Shell Programming and Scripting

Check for file existence using wildcards

I am using the following command to check for files on a Unix (Solaris 9) and on Linux: if (-r *.) then echo " las file found" else echo " no las file found" endif If no las file is present, the "no las file found" message is displayed. If a las file is present, however, I get... (9 Replies)
Discussion started by: phudgens
9 Replies

5. Shell Programming and Scripting

How to check for file existence?

i want to check if the file is in the directory or not, and also it should be handle error conditions, like missing files and report the error and exit. i did something like this: file ="hello" if !test -e "${file}" then echo "No such files exist!" exit 1 else do something....... fi ... (1 Reply)
Discussion started by: mingming88
1 Replies

6. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

7. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies

8. AIX

how to check the existence of a file during ftp using korn shell?

i can able to transfer a file from build server(AIX)to webserver using ksh through ftp.my query is to check the existence of file while transfering from one server to other .i.e i need some command or script that checks the existence of file with same name in both server,within ftp syntax. ... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

9. Shell Programming and Scripting

Csh to check for existence of file

Hi, I would like to check the existence of files (doesn;t matter the number of files) in a directory. My file is named in the following manner (always ending with " myfile "). Can anybody give me some guidance? EG: abc1_myfile sdfr_myfile sffgd_myfile and so on ...... My... (9 Replies)
Discussion started by: Raynon
9 Replies

10. Solaris

How to check the file existence using shell scripting in Solaris-10

Hi, I have a script which will check the fiel existence, the lines are as below if !(test -d ./data) then mkdir data fi In the first line error occurs as below generatelicense.sh: syntax error at line 2: `!' unexpected Where as this script works fine in linux OS. How to solve... (2 Replies)
Discussion started by: krevathi1912
2 Replies
Login or Register to Ask a Question