[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using Find and verifying directory before continuing search

Posted on 2004-10-13
5
Medium Priority
?
270 Views
Last Modified: 2010-04-21
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
Comment
Question by:elwayisgod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 1000 total points
ID: 12305609
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
 
LVL 11

Expert Comment

by:avizit
ID: 12305656
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
 
LVL 11

Expert Comment

by:avizit
ID: 12305659
oops change that to

if [[ ! -d $1 ]] ; then echo $1 is not a directory ; fi
0
 
LVL 38

Expert Comment

by:yuzh
ID: 12305793
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
 
LVL 2

Expert Comment

by:stokesj56
ID: 12324859
if [[ ! -d $1 ]]
then
    echo "Directory not found: $1" >&2
    exit 1
fi
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question