#!/usr/bin/perl -w # # addPath: add a path item to a given variable, delimited by colons, if # not already there. Warn if it doesn't exist as a directory. # # # Usage: # addPath varname dir # Typically: # PATH=`addPath PATH /my/bin` use strict; if ($ARGV[0] =~ m/^-+h/) { system "perldoc addToPath"; exit; } my $var = $ARGV[0]; my $dir = $ARGV[1]; if ($ARGV[1] eq "") { die "Must specify env variable to extend, and path to add.\n"; } my $oldvar = $ENV{$var}; if (-d $dir == 0) { warn "\n'$dir' is not a directory, not added to \$$var.\n"; exit; } if (":$var:" =~ /:$dir:/) { exit; # already there } $var = "$oldvar:$dir"; print "$var"; exit; sub showUsage { print " =head1 Usage PATH=`addPath PATH [path-to-add] Adds the given path to the end of any path variable (shown as PATH, but CDPATH, CLASSPATH, MANPATH, etc. can be substituted). Will not add if it's already there. =head1 Warning You need to do it with `` as shown, because the script can't change the actual variable directly for the shell it's called from. "; }