home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dove!dove.nist.gov!przemek
- From: przemek@rrdstrad.nist.gov (Przemek Klosowski)
- Newsgroups: comp.lang.perl
- Subject: handling symbolic links
- Message-ID: <PRZEMEK.92Aug21141948@rrdstrad.nist.gov>
- Date: 21 Aug 92 18:19:48 GMT
- Sender: news@dove.nist.gov
- Organization: U. of Maryland/NIST
- Lines: 68
-
-
- Hello!
-
- Here's a perl script to handle symbolic links to software distribution
- directories. A baroque solution to a simple problem? maybe; but I did
- fail on several occasions to stick to my own style of disk management
- because of those pesky links that cannot be renamed but have to be
- deleted.
-
- BTW, I did not mean to suggest that perl-4.035 is buggy; the example
- directory used below was chosen at random. In fact, I did delete the old
- directory :^)
-
- #!/usr/local/bin/perl
- # Copyright (c) 1992 Przemek Klosowski at NIST. All Rights Reserved
- # Filename: relink
- # Usage: relink symlink-name [target-name]
- # Does remember where did the old link go, and tries to guess the new one
- # $Id$
- # Rationale: When updating software, one may still want to keep the old
- # version around, in case the new one is buggy. In such cases
- # one may want to have a symlink directory pointing
- # to the latest distribution directory:
- # perl@ -> perl-4.035
- # perl-4.035/
- # perl-4.019/
- # This makes the configuration easier, since one can always use
- # the directory 'perl' as a location of the most recent version.
- # Maintaining links manually is tedious, as one first has to delete
- # the link and then recreate it with a new name.
- #
- # This script is useful in such cases; one may call it to set up
- # the initial link ('relink perl perl-4.019'); later, when one
- # installs the new version, one would just say 'relink perl' to
- # to change the link to point to perl-4.035 (if there is a unique
- # new target directory, it is used as the default; otherwise one
- # would specify it as in the example 4 lines above.
-
- # History: przemek - Aug 20, 1992: Created.
-
- ($symlink, $newdir) = @ARGV;
-
- unless (-l $symlink) {
- -e _ && die "relink: $symlink is not a symbolic link ($!)\n";
- }
- # if we're here, $symlink either does not exist yet or is a genuine symlink
- unless($newdir) {
- # Weird stuff. Only $newdir is being changed
- ($newdir = ($target = readlink($symlink))) =~ s/[-.0-9]*$//;
- chop (@candidates = `ls -d $newdir*`);
- @candidates = grep(!/^(($target)|($symlink))[\/]?$/, @candidates);
- $#candidates == $[ || # could be either too little or too many
- die "Please specify target for symbolic link '$symlink'\n";
- $newdir = $candidates[$[];
- }
- unlink($symlink);
- symlink($newdir,$symlink);
-
-
- ==================end of program=================================
-
- --
- przemek klosowski (przemek@rrdstrad.nist.gov)
- Reactor Division (bldg. 235), E111
- National Institute of Standards and Technology
- Gaithersburg, MD 20899, USA
-
- (301) 975 6249
-