Path modifier script

I just discovered that XQuartz on Mountain Lion does some annoying things to my PATH, changing:

/sw/lib/perl5/ExtUtils:/sw/bin:/sw/sbin:
/usr/bin:/bin:/usr/sbin:/sbin:
/usr/local/bin:
/opt/X11/bin:/Users/hansen/bin

in a Terminal window to

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:
/opt/X11/bin:
/sw/lib/perl5/ExtUtils:/sw/bin:/sw/sbin:
/usr/X11R6/bin:
/Users/hansen/bin:/Users/hansen/bin:
/opt/X11/bin

in an xterm.

The duplicate entries aren’t so bad, but moving the system directories in front of Fink’s is annoying.  Running pathsetup.sh again doesn’t change this, unfortunately.  I came up with a script which lets me reorder the PATH by blocks and remove via a pattern match any items I don’t want.  As an afterthought, it removes duplicates, too.  By running

export PATH=`pathformat.pl --delete=X11R6`

I am able to restore the PATH settings to the xterm that I have in my Terminal window. The pathformat.pl script follows:

#!/usr/bin/env perl -w

# Read in the PATH environment variable and
# 1) Strip out duplicate entries
# 2) Delete entries matching a pattern given by instances of the --delete flag
# 3) Put the entries in the order Fink, system, user.
# This could certainly be extended.

use strict;
use List::MoreUtils qw { uniq };
use Getopt::Long;

my @delete_vals;
my $result=GetOptions ("delete=s" => \@delete_vals);

my (@finkpath, @userpath, @generalpath);

ENTRY: foreach my $entry (uniq (split /:/, $ENV{"PATH"})) {
     foreach (@delete_vals) {
          next ENTRY if $entry =~ /$_/;
     }
     if ($entry =~ m|^/sw|) {
          push @finkpath, ($entry);
     } elsif ($entry =~ m/$ENV{"HOME"}/) {
          push @userpath, ($entry);
     } else {
          push @generalpath, ($entry);
    }
}
print join(':', @finkpath, @generalpath, @userpath),"\n";
About these ads

Tags: , ,

4 Responses to “Path modifier script”

  1. finkakh Says:

    Reblogged this on Finkers: a blog about Fink.

  2. finkakh Says:

    The following is convenient:

    1) I created a script called “fixpath.sh” in /sw/etc/profile.d with contents:

    #!/bin/sh -ev
    export PATH=`/Users/finkakh/bin/pathformat.pl`

    . The name isn’t important, because fink’s environment initialization (init.sh or init.csh) sources everything in that directory.

    2) I set the Terminal item in the X11 Applications menu to be “xterm -ls”, i.e. ‘login shell’, so that init.sh and profile.d/ get processed. (The default on Xquartz is now to start with whatever the user’s PATH setting is, and older X11 releases just ignored it–this gives consistent results for both cases)

  3. finkakh Says:

    I’ve made up a Fink package for this called “fixpath”–I renamed the Perl script to “fixpath.pl”. There’s a github repository for it:

    https://github.com/akhansen/fixpath

  4. fixpath-1.0.0 « akh's Fink Blog Says:

    [...] all supported OS X.  This contains the path-fixing script that I mentioned [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: