• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Need script to rebuild soft links

Here's the problem,

When I refesh a developement environment from a tape backup of production, there are a lot of softlinks in many different subdirectories that need to be rebuilt, so it links to the right files.

Can I get a script that can take the directory name that needs changed, the directory name that it needs changed to, and the full path to the directory so any subdirectorieswith bad links will get fixed.

here's an example of what I'm talking about...

in

/ORACLE/apps/DEV1/eper/1.0.0/bin

there are many links like

lrwxrwxrwx   1 applmgr    apps            57 Mar 30 13:05 HRPSRSRE -> /ORACLE/apps/PROD1/fnd/6.1.1/bin/fndcpesr

Basically I need a quick way of changing all links from PROD1 to DEV1 (The sub-directory structures are the same between production and developement)

Ideally, the command would look something like

change_links.sh PROD1 DEV1 /ORACLE/apps/DEV1

I'm running on HPUX 10.20 if that helps.

If I am unclear about anything please let me know.

And please post comments, not answers, I will accept your comment as an answer, when I verify that everything works.


0
mboss
Asked:
mboss
  • 2
  • 2
1 Solution
 
tfewsterCommented:
change_links.sh:

#!/bin/sh
# Usage: change_links.sh OLDDIR NEWDIR LINK

OLDDIR=$1
NEWDIR=$2
LINK=$3

# Check links
echo
echo Link $LINK
echo `ls -l $LINK | awk '{ print $9 $10 $11}'`
echo becomes
echo `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g`

# Change links
#ln -f -s `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g` $LINK


To use this, call it from a "find", e.g.
find /oracle/apps/DEV1 -type l -exec ~/test/change_links.sh PROD1 DEV1 {} \;

Note that it just tests the links for now, & echos what it finds + what it WOULD do when you
uncomment the "ln ..." line

It will go wrong if you have a directory PROD1 under DEV1...
0
 
tfewsterCommented:
I've put an addditional check in, so if the links found don't contain the string $OLDDIR, they are just ignored (My previous script would have rewritten them, which is pointless & asking for trouble...)

change_links.sh:

#!/bin/sh
# Usage: change_links.sh OLDDIR NEWDIR LINK

OLDDIR=$1
NEWDIR=$2
LINK=$3

#test if $LINK contains string $OLDDIR
TARGET=`ls -l $LINK | awk '{ print $ 11}'|grep $OLDDIR`
echo $TARGET

if [ ! X$TARGET = X ]
then
  # Check links
  echo
  echo Link $LINK
  echo `ls -l $LINK | awk '{ print $9 $10 $11}'`
  echo becomes
  echo `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g`

  # Change links
  #ln -f -s `ls -l $LINK | awk '{ print $11}' | sed s/$OLDDIR/$NEWDIR/g` $LINK
fi
0
 
mbossAuthor Commented:
Excellent.  Thanks for all your help.  Can you recommend a good book/site to learn sed and awk?  

I raised the points to 300 cause you deserve it.

Thanks again

mboss
0
 
mbossAuthor Commented:
woops...I think I forgot to rasie the points like I said I would.  I'll post them for you an a sec.

Sorry about that.

mboss
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now