Solved

Using Find and verifying directory before continuing search

Posted on 2004-10-13
5
232 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
  • 2
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now