Enter Perl and a small about of shell scripting and I have an interactive way of toggling directories in my path (or any "path like" variable) on or off and adding directories to the beginning and end of my path.
#!/usr/bin/perl
use strict;
my $variableName = shift;
if ( !$variableName ) {
$variableName = "PATH";
}
my $home = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($<))[7];
my $fName = "$home/.setpath.out";
if ( -e $fName ) {
unlink ( $fName )
or die( "Unable to unlink previous $fName file ($!)!\n" );
}
my @directories;
if ( !exists $ENV{$variableName} ) {
print( "No variable, $variableName, in the environment. Continue [Y/
+n]? " );
my $result;
do {
$result = <STDIN>;
chomp( $result );
if ( !defined($result) || lc($result) eq "n" ) {
exit( 1 );
}
} while ( lc($result) ne "y" && $result ne "" );
} else {
@directories = split( /:/, $ENV{$variableName} );
}
my $dirty = 0;
while( 1 ) {
print( "Your current $variableName contains:\n" );
for ( my $i = 0; $i < scalar(@directories); ++$i ) {
my $directory = $directories[ $i ];
my $state = "on";
if ( $directory =~ /\.off$/ ) {
$state = "off";
$directory =~ s/\.off$//;
}
printf( " %2d) [%3s] %s\n", $i, $state, $directory );
}
print(
'-----
Enter:
t<n> to toggle a directory on/off,
[a|A]<directory> to add a new directory (to front|end)
q to quit.
> ' );
my $commandLine = <STDIN>;
if ( !defined($commandLine) ) {
exit( 1 );
}
chomp( $commandLine );
$commandLine =~ /(.)(.*)/;
my ($command, $args) = ($1, $2);
if ( $command eq "t" ) {
if ( !defined($args) || $args eq "" ||
$args >= scalar(@directories) || $args < 0 ) {
print( "$args is not a valid directory number (0 .. ", scalar(@d
+irectories), ")\n\n" );
} else {
$dirty = 1;
if ( $directories[ $args ] =~ /\.off$/ ) {
$directories[ $args ] =~ s/\.off$//;
} else {
$directories[ $args ] .= ".off";
}
print( "\n" );
}
} elsif ( $command eq "A" ) {
push( @directories, $args );
$dirty = 1;
} elsif ( $command eq "a" ) {
splice( @directories, 0, 0, $args );
$dirty = 1;
} elsif ( $command eq "q" ) {
if ( $dirty ) {
open( OUT, "> $fName" )
or die( "Unable to open $fName for writing ($!)!\n" );
print OUT ("export $variableName=", join(":", @directories), "\n
+");
close( OUT );
exit 0;
} else {
print( "No changes made to $variableName.\n" );
exit( 1 );
}
}
}
Pretty much useless by itself as it doesn't actually change the path in any way. What it needs is a little bit of shell scripting to tie things together nicely. The following is for bash.
function setpath { /fully/qualified/path/to/setpath.pl "$1"; if [ $? =
+= 0 ]; then . $HOME/.setpath.out; echo "$1 updated."; fi };
Now I can safely play around with my PATH, LD_LIBRARY_PATH, etc... to my hearts content.