Solved

Using Find and verifying directory before continuing search

Posted on 2004-10-13
5
242 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 36
How to Change Interface on Cisco IOU 11 85
help pulling data string using Awk 9 63
mobaxterm not able to change directory 28 109
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

895 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

21 Experts available now in Live!

Get 1:1 Help Now