• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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
0
elwayisgod
Asked:
elwayisgod
  • 2
  • 2
1 Solution
 
yuzhCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now