Operating System - Linux
1752720 Members
5672 Online
108789 Solutions
New Discussion юеВ

Re: Processing directory structures

 
Steve Givens
Occasional Advisor

Processing directory structures

I need some help processing directory structures. I have an environment variable which contains the following (example):

admin/sql
admin/bin
admin/SID
admin/SID/adump
admin/SID/bdump
admin/log
archive
archive/SID
archive/SID/dmp

I want to process (actually build a "cp -R" command) for those entries that do not contain SID or those that contain SID but only as the last word. I don't want to process the entries that contain SID but have something after it (e.g. - SID/dmp).

My current code is attached. I'm not sure how to check for the last condition of something after SID.

I would imagine there is also probably a better way of handling the from and to directories so that I don't have to use 2 variables.

Thanks in advance for your help.

Steve
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: Processing directory structures

Shalom Steve,

Nice name.

Your code crashes my browser so I can't look.

basename

That will isolate the last variable and can be used to extract it into another variabel.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steve Givens
Occasional Advisor

Re: Processing directory structures

Not sure why the attachment would crash your browser, but here is the code in question:

#!/bin/ksh
ORACLE_SID=${ORACLE_SID:-\wiltst1} #Default ORACLE_SID
SCRIPT_DIR=${SCRIPT_DIR:-\/oracle} #Default directory for scripts

FromDirNames=`find ${SCRIPT_DIR}/OB -type d | tail +2 | sed 's,'${SCRIPT_DIR}'/OB/,,'`
ToDirNames=`echo ${FromDirNames} | sed 's,SID,'${ORACLE_SID}',g'`

echo "\n\nFromDirNames=${FromDirNames}\n"
echo "\n\nToDirNames=${ToDirNames}\n"

for subdir in ${ToDirNames}
do
if [ -d "${ORACLE_BASE}/$subdir" ] ; then
echo "${ORACLE_BASE}/$subdir already exists"
else
echo "Creating ${ORACLE_BASE}/$subdir"
Cmd="cp -R ${SCRIPT_DIR}/OB/$subdir ${ORACLE_BASE}/$subdir"
FinalCmd="`echo ${Cmd} | sed 's/'${ORACLE_SID}'/SID/1'`"
echo "\t Cmd=${Cmd}"
echo "\t FinalCmd=${FinalCmd}"
fi
done # for subdir loop
Mel Burslan
Honored Contributor

Re: Processing directory structures

did you try using grep to weed out the entries you do not want like


find ${SCRIPT_DIR}/OB -type d | grep -v SID$

this should eliminate tje lines ending with "SID" from the find output.
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Processing directory structures

Hi Steve:

Given that you have a variable loaded with (space-delimited) directories, like:

# echo ${VAR}
admin/sql admin/bin admin/SID admin/SID/adump admin/SID/bdump admin/log archive
archive/SID archive/SID/dmp

# echo ${VAR}|perl -nle '@a=split / /;for (@a) {print if m%/SID\z%;print unless m%SID%}'

...will yield:

admin/sql
admin/bin
admin/SID
admin/log
archive
archive/SID

...You could capture this output too:

# NEWVAR=`echo ${VAR}|perl -nle '@a=split / /;for (@a) {print if m%/SID\z%;print unless m%SID%}'`

Regards!

...JRF...

Sandman!
Honored Contributor

Re: Processing directory structures

imho...modify the find command instead of parsing the list of directories i.e.

change...

FromDirNames=`find ${SCRIPT_DIR}/OB -type d | tail +2 | sed 's,'${SCRIPT_DIR}'/OB/,,'`

to...

FromDirNames=`find ${SCRIPT_DIR}/OB ! -path "${SCRIPT_DIR}/OB/*/SID/*" -type d | tail +2 | sed 's,'${SCRIPT_DIR}'/OB/,,'`
Steve Givens
Occasional Advisor

Re: Processing directory structures

I knew I'd have a Perl fan respond to this, so thanks.

What I'm working on is a deployment script that needs to run on any environment. Right now I just need to worry about Sun, AIX and HP-UX. Any concerns with Perl compatibility?
Peter Nikitka
Honored Contributor

Re: Processing directory structures

Hi,

ANY environment - you are really aiming high :-)

But the UNIXs you mentioned
- as long as only such one liners are in play, they can be adopted to ANY perl-version
- as long as these OSs are in play, recent perl distributions of the same perl version are found at

http://activestate.com

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Steve Givens
Occasional Advisor

Re: Processing directory structures

Mel, actually I'm trying to do the opposite - keep lines with no SID in it and lines that end in SID.

Sandman, your syntax is not working for me:
"-path is not a valid option". Is that not supported on AIX?
James R. Ferguson
Acclaimed Contributor

Re: Processing directory structures

Hi Steve:

No, the '-path' switch is not valid with AIX's 'find'. If you want easy portability, you want Perl.

Using Perl, you could leverage the 'File::Find' core module if you want.

Regards!

...JRF...