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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if a filename exists and act accordingly

Status
Not open for further replies.

gvidos

Programmer
Jul 24, 2002
17
0
0
GR
Hi,

I need to check if a file exists and only then execute it.

for example (in C format)
file="myfile"
if(file exists)
system("file")

Thanks
 
hi,

do you want this in shell script ?
if so ,

if [ -f /tmp/filename ]
then
print " it exists execute file "
/tmp/filename
else
print " not exist "
fi
 
You may want to check on if the file in question has execute rights. You do that with the -x option:
Code:
#!/bin/sh

if [ -x $file ]
then
  print " $file exists - executing..."
  $file
else
  if [ -f $file ]
    print " $file exists - but I need to pass it to the shell to run..."
    /bin/sh $file
  fi
fi
Hope this helps. Einstein47
("For every expert, there is an equal and opposite expert." - Arthur C. Clarke)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top