> export PATH=/my/new/applications/pathinstead of this:
> export PATH=/my/new/applications/path:$PATHone 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:
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.#!/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 ); } } }
Now I can safely play around with my PATH, LD_LIBRARY_PATH, etc... to my hearts content.function setpath { /fully/qualified/path/to/setpath.pl "$1"; if [ $? = += 0 ]; then . $HOME/.setpath.out; echo "$1 updated."; fi };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl Path Editor for Unix
by graff (Chancellor) on Sep 14, 2005 at 04:58 UTC | |
by greenFox (Vicar) on Sep 14, 2005 at 07:53 UTC | |
by 5p1d3r (Hermit) on Sep 14, 2005 at 13:09 UTC | |
Re: Perl Path Editor for Unix
by bluto (Curate) on Sep 13, 2005 at 21:23 UTC | |
Re: Perl Path Editor for Unix
by QM (Parson) on Sep 13, 2005 at 20:39 UTC | |
Re: Perl Path Editor for Unix
by duelafn (Parson) on Sep 15, 2005 at 16:00 UTC | |
Re: Perl Path Editor for Unix
by scollyer (Sexton) on Sep 28, 2005 at 14:26 UTC | |
by Anonymous Monk on Aug 23, 2012 at 17:21 UTC | |
by jdporter (Paladin) on Aug 23, 2012 at 19:31 UTC | |
by Anonymous Monk on Sep 04, 2012 at 12:56 UTC | |
by Anonymous Monk on Aug 31, 2012 at 20:47 UTC |