Using Find and verifying directory before continuing search

I invoke the script below by typing:
./findfile.ksh /user/home/stoteve myfile1 myfile 2 myfile3

I can have as many files to search for as I want.  How do I modify the code to verify that the directory in argument $1 exists before continuing.  Then if it does not exist a error message prints and the system aborts.  

#!/bin/ksh

DIR=$1
shift

OPTS="-name '$1'"
shift

while [[ "$1" != "" ]]
do
OPTS="$OPTS -o -name '$1'"
shift
done

eval "find $DIR $OPTS" 2>/dev/null
elwayisgodAsked:
Who is Participating?
 
yuzhConnect With a Mentor Commented:
Do you mean at least to have oncommandline arg?

you can do:

if [ $# -eq 0 ] ; then
   echo " usage: $0 args"
   exit 1
fi
DIR=$1
....
0
 
avizitCommented:
yuo can check if a value is indeed a directory by

if [[ ! -d $1 ]] ; then echo /dir/name is not a directory ; fi


check http://www.bolthole.com/solaris/ksh-builtins.html  for other checks
0
 
avizitCommented:
oops change that to

if [[ ! -d $1 ]] ; then echo $1 is not a directory ; fi
0
 
yuzhCommented:
If $1 use FULL path then you can do

if [ ! -d $1 ] ; ....

otherwise, you need to use find command to locate the dir first.
0
 
stokesj56Commented:
if [[ ! -d $1 ]]
then
    echo "Directory not found: $1" >&2
    exit 1
fi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.