path_uniq (3214B)
1 #! /bin/perl 2 3 ######################################################################## 4 # 5 # /u/sonmi/bin/path_uniq 6 # 7 # this script makes components of a PATH like string unique cand prints 8 # it to stdout 9 # 10 # parameters 11 # ---------- 12 # PATH 13 # 14 # options 15 # ------- 16 # -d delimiter - default : 17 # -s shortens the path 18 # 19 # usefull enhancements: in the usage part, try to guess what was meant as 20 # a path and echo it to stdout to not break for PATHs with blanks 21 # 22 ######################################################################## 23 24 sub usage { 25 print STDERR "usage $0 [-s] [-d <delimiter>] PATH\n"; 26 print STDERR " this script makes components of the PATH unique, if you\n"; 27 print STDERR " pass in a searchpath A:B:C:A:B:E it will print A:B:C:E to\n"; 28 print STDERR " the stdout\n\n"; 29 print STDERR " -s will mercylessly cut components from the path, \n"; 30 print STDERR " use at your own risk\n\n"; 31 print STDERR " the parameters you gave were: \n"; 32 for ( $i = 0; $i <= $#ARGV; $i++ ) { 33 print STDERR " $ARGV[$i]\n"; 34 } 35 exit ; 36 } 37 38 39 $i = 0; 40 $j = 0; 41 $delimiter = ":"; 42 $searchpath = ""; 43 @pathcomponents; 44 $found=0; 45 $newpath=""; 46 $shorten=0; 47 48 for ( $i=0; $i <= $#ARGV; $i++) { 49 if ( $ARGV[$i] eq '-d' ) { 50 $delimiter = $ARGV[++$i]; 51 } elsif ( $ARGV[$i] eq '-s' ) { 52 $shorten=1; 53 } else { 54 $searchpath = $ARGV[$i]; 55 } 56 } 57 if ( $searchpath eq "" ) { 58 usage; 59 } 60 #print STDERR "delimiter $delimiter\n"; 61 #print STDERR "shorten $shorten\n"; 62 #print STDERR "searchpath $searchpath\n"; 63 64 @pathcomponents=split($delimiter, $searchpath); 65 66 for ( $i = 0; $i <= $#pathcomponents; $i++ ) { 67 $found=0; 68 if ( $shorten == 1 ) { 69 if ( "\/tools\/ns-arch\/sparc_sun_solaris2\.4\/lib\/sparcworks\/SUNWspro/bin" eq $pathcomponents[$i] || 70 "\/h\/tortoise\/export\/share\/builds\/tools\/sparc_sun_solaris2\.5\.1\/perl5\.004\/bin" eq $pathcomponents[$i] || 71 "\/usr\/dist\/local\/exe" eq $pathcomponents[$i] || 72 "\/opt\/SUNWspro\/bin" eq $pathcomponents[$i] || 73 "\/opt\/SUNWwabi\/bin" eq $pathcomponents[$i] || 74 "\/u\/svbld\/bin" eq $pathcomponents[$i] || 75 "\/usr\/demos" eq $pathcomponents[$i] || 76 "\/usr\/audio\/bin" eq $pathcomponents[$i] || 77 "\/usr\/openwin\/demo" eq $pathcomponents[$i] || 78 "\/tools\/contrib\/bin" eq $pathcomponents[$i] || 79 "\/usr\/etc\/" eq $pathcomponents[$i] || 80 "\/usr\/demos\/bin" eq $pathcomponents[$i] ) { 81 82 83 #print "dumped: $pathcomponents[$i]\n"; 84 next; 85 } 86 #print "keep: $pathcomponents[$i]\n"; 87 } 88 for ( $j = 0; $j < $i; $j++ ) { 89 if ( $pathcomponents[$j] eq $pathcomponents[$i] ) { 90 #print "$i and $j match - $pathcomponents[$i] - $pathcomponents[$j]\n"; 91 $found=1; 92 last; 93 } 94 } 95 if ( $found == 0 ) { 96 #print "$pathcomponents[$i]:"; 97 if ($i == 0) { 98 $newpath = $pathcomponents[$i]; 99 } else { 100 $newpath=join($delimiter, $newpath,$pathcomponents[$i]); 101 } 102 } 103 } 104 print "$newpath\n"; 105 exit; 106 107