Solved

Need script to rebuild soft links

Posted on 2000-04-12
4
259 Views
Last Modified: 2010-04-21
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
Comment
Question by:mboss
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
tfewster earned 250 total points
ID: 2708229
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
 
LVL 20

Expert Comment

by:tfewster
ID: 2708333
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
 

Author Comment

by:mboss
ID: 2708357
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
 

Author Comment

by:mboss
ID: 2708370
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

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

757 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

18 Experts available now in Live!

Get 1:1 Help Now