home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5417 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  3.0 KB

  1. Path: sparky!uunet!dove!dove.nist.gov!przemek
  2. From: przemek@rrdstrad.nist.gov (Przemek Klosowski)
  3. Newsgroups: comp.lang.perl
  4. Subject: handling symbolic links
  5. Message-ID: <PRZEMEK.92Aug21141948@rrdstrad.nist.gov>
  6. Date: 21 Aug 92 18:19:48 GMT
  7. Sender: news@dove.nist.gov
  8. Organization: U. of Maryland/NIST
  9. Lines: 68
  10.  
  11.  
  12. Hello!
  13.  
  14. Here's a perl script to handle symbolic links to software distribution
  15. directories.  A baroque solution to a simple problem? maybe; but I did
  16. fail on several occasions to stick to my own style of disk management
  17. because of those pesky links that cannot be renamed but have to be
  18. deleted.
  19.  
  20. BTW, I did not mean to suggest that perl-4.035 is buggy; the example
  21. directory used below was chosen at random. In fact, I did delete the old
  22. directory :^)
  23.  
  24. #!/usr/local/bin/perl
  25. # Copyright (c) 1992 Przemek Klosowski at NIST. All Rights Reserved
  26. # Filename: relink
  27. # Usage:    relink symlink-name [target-name]
  28. #           Does remember where did the old link go, and tries to guess the new one
  29. # $Id$
  30. # Rationale: When updating software, one may still want to keep the old 
  31. #            version around, in case the new one is buggy. In such cases
  32. #            one may want to have a symlink directory pointing
  33. #            to the latest distribution directory:
  34. #                   perl@ -> perl-4.035
  35. #                   perl-4.035/
  36. #                   perl-4.019/
  37. #            This makes the configuration easier, since one can always use
  38. #            the directory 'perl' as a location of the most recent version.
  39. #            Maintaining links manually is tedious, as one first has to delete 
  40. #         the link and then recreate it with a new name.
  41. #  
  42. #            This script is useful in such cases; one may call it to set up
  43. #            the initial link ('relink perl perl-4.019'); later, when one 
  44. #            installs the new version, one would just say 'relink perl' to
  45. #            to change the link to point to perl-4.035 (if there is a unique
  46. #            new target directory, it is used as the default; otherwise one
  47. #            would specify it as in the example 4 lines above.
  48.  
  49. # History:              przemek - Aug 20, 1992: Created.
  50.  
  51. ($symlink, $newdir) = @ARGV;
  52.  
  53. unless (-l $symlink) {
  54.     -e _ && die "relink: $symlink is not a symbolic link ($!)\n";
  55. }
  56. # if we're here, $symlink either does not exist yet or is a genuine symlink
  57. unless($newdir) {
  58.     # Weird stuff. Only $newdir is being changed
  59.     ($newdir = ($target = readlink($symlink))) =~ s/[-.0-9]*$//;
  60.     chop (@candidates = `ls -d $newdir*`);
  61.     @candidates = grep(!/^(($target)|($symlink))[\/]?$/, @candidates);
  62.     $#candidates == $[ ||    # could be either too little or too many
  63.     die "Please specify target for symbolic link '$symlink'\n";
  64.     $newdir = $candidates[$[];
  65. }
  66. unlink($symlink);
  67. symlink($newdir,$symlink);
  68.  
  69.  
  70. ==================end of program=================================
  71.  
  72. --
  73.             przemek klosowski (przemek@rrdstrad.nist.gov)
  74.             Reactor Division (bldg. 235), E111
  75.             National Institute of Standards and Technology
  76.             Gaithersburg, MD 20899,      USA
  77.  
  78.             (301) 975 6249
  79.