Re: Processing directory structures (210 Views)
Reply
Occasional Advisor
Steve Givens
Posts: 14
Registered: ‎05-18-2004
Message 1 of 10 (210 Views)

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
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 2 of 10 (210 Views)

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
Please use plain text.
Occasional Advisor
Steve Givens
Posts: 14
Registered: ‎05-18-2004
Message 3 of 10 (210 Views)

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
Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,213
Registered: ‎08-26-1998
Message 4 of 10 (210 Views)

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...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 10 (210 Views)

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...

Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 6 of 10 (210 Views)

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/,,'`
Please use plain text.
Occasional Advisor
Steve Givens
Posts: 14
Registered: ‎05-18-2004
Message 7 of 10 (210 Views)

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?
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 8 of 10 (210 Views)

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"
Please use plain text.
Occasional Advisor
Steve Givens
Posts: 14
Registered: ‎05-18-2004
Message 9 of 10 (210 Views)

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?
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 10 (210 Views)

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...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation