Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found