After doing this:
> export PATH=/my/new/applications/path
instead of this:
> export PATH=/my/new/applications/path:$PATH
one too many times I decided to do something about it.

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.

The first part of this little utility is the setpath.pl script:

#!/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.

In reply to Perl Path Editor for Unix by 5p1d3r

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.