Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check for a file and exit if not found

Status
Not open for further replies.

navink123

Programmer
Sep 2, 2003
21
US
I have a job that runs everyday. It looks for a file in a particular directory and takes it as an input file.
This file is generated by some previous job.
Sometimes this previous job does not run depending on various conditions that are unable to predict. But then
my job still looks for that file and fails if it cannot find the file.
Is there any way to check for this file before my job run and simply exit saying file not found, instead of failing.

Thanks
N
 
# dependeing on your file type - "man test"...
if [ ! -f path2File ] ; then
echo "file doesn't exist"
exit 1
fi;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You migfht try the following &quot;launcher&quot; script
as a way to get the result you want.

(This assumes the Korn shell, but yout might want
to use some other shell, with slightly different grammar,
for the &quot;if&quot; statement, and a slightly
different grammar for the 'file test&quot;, both in lione-15.

Notice - there is a &quot;dot&quot; (period) , followed by
a blank space preceeding your $1
in line # 19. This is REQUIRED, so that
<your-progrqm-name> &quot; will &quot;recognize&quot; the
&quot;command line&quot; variables ($1, $2) of
launcher.

Also, the line-numbers, at the start of each
line of the program are NOT a part of the program
and should be omitted by you.

I put them in so that you can &quot;follow&quot; the lines better.
You can OMIT alll the lines that are comments,
line-3 to line-14, since these lines are there merely to explain what-is-what, in this example.

The 'launcher&quot; program does just what it says
- it &quot;launches&quot; some other program, $1,
(for you) and could
be used to '&quot;launch&quot; alomost any program that
requires a single, input data file., $2.

(See my instructions, at the bottom, about how to use
the &quot;launcher&quot; script.)
=======================================
1 #!/bin/ksh
2 # program: &quot;launcher&quot;
3 # Purpose: execute the
4 # program: <program-name>
5 # If (and only if) file $1
6 # is present.
7 # -----------------------------------------
8 # NOTICE the &quot;dot&quot;-and-space
9 # preceding the <program-name>
10 # so that YOUR (original) program
11 # will use the SAME
12 # &quot;command-line-parameter&quot;
13 # as the &quot;launcher&quot; program.
14 # --------------------------------
15 if [[ -f $1 ]]
16 then
17 # -------------------------------
18 # Now we cqn excute..
19 . $1 $2
20 # ---------------------------------
21 else
22 # We CAN'T execute, since our
23 # file is NOT present
24 echo &quot;File $1: Not found. Can't run now.&quot;
25 echo &quot;Exit 99&quot;
26 exit 99
27 fi
=======================================
How you run the program:

launcher <your-program-name> <the-required-file-name> <enter>
=======================================
The <your-program-name> is the &quot;$1&quot;, in the
&quot;launcher&quot; program, The
<required-file-name> is the &quot;$2&quot; in the
&quot;launcher&quot; program.
========================================There are other ways to accomplish what you want, also.

You could also put that same &quot;if&quot; test into your
own program, where the &quot;if&quot; statement would
&quot;inclose&quot; the rest of your program, for execution, if
the file were present, and would &quot;exit&quot; if not.

If you want an example of this, ask...
have a nice life, from ernieah


End-of-memo: Best to you..
from ernieah.
 
Hi ernieah,
Thanks for the answer.
Can you please post the other example as well.

its always better to have more than one options. U never know which may work better.

Thanks,
N
 
One liner...
Code:
[ ! -f ${FNAME} ] && print &quot;File ${FNAME} not found!&quot; && exit 1
Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top