delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/07/28/23:51:47

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: "Gary R. Van Sickle" <g DOT r DOT vansickle AT worldnet DOT att DOT net>
To: "Cygwin mailing list" <cygwin AT cygwin DOT com>
Subject: Useful Cygwinism #2: PATH Despacifier/Deduplicateifier
Date: Sun, 28 Jul 2002 22:51:43 -0500
Message-ID: <NCBBIHCHBLCMLBLOBONKCEOEDCAA.g.r.vansickle@worldnet.att.net>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Importance: Normal

I've been noticing a lot of traffic lately on people having problems with paths
which contain spaces, admonitions to "keep your PATH as short as possible for
performance reasons", etc.  So in the spirit of floating everyone's boat, here's
installment #2 of "Useful Cygwinisms".

PROBLEM:
Your Windows PATH contains paths with spaces in them, causing you all manner of
grief.  It also contains the Windows paths to /bin and /usr/local/bin, to make
running Cygwin apps from the 9x/NT command line easier, but these end up getting
needlessly duplicated when you run a bash session, due to the actions of
/etc/profile.

SOLUTION:
A little Perl script I like to call "despaceify":

=========================================================
#!/usr/bin/perl -w
use strict;
my @pathcomps;
my @pathcomps_out;
my %comps_seen_already;
@pathcomps = split(/:/, shift);
for(@pathcomps)
{
        if(!exists($comps_seen_already{$_}))
        {
                my $path;
                $path='"'.$_.'"';
                $path=qx(cygpath -u \$(cygpath -ws $path));
                $path=~tr/\n//d;
                push(@pathcomps_out, $path);
        }
        $comps_seen_already{$_}=1;
}
print join(":", @pathcomps_out);
==========================================================

This script takes a ":"-separated Unixoid PATH, replaces any component paths
containing spaces with their equivalent "8.3" spaceless replacements, and also
removes any duplicates (only the first instance of a particular path will
remain).  To make it work its magic, just save the script as "despaceify"
somewhere convenient (for me, ~/bin/despaceify), of course make sure you have
Perl installed, and then add this to your .bash_profile:

# Clean up PATH.
export PATH=$(~/bin/despaceify "$PATH");

And that's it.  Every time you start up a bash session, your PATH will be
automatically scrubbed clean!

COMMENTS:
This addition adds a slightly annoying ~1-2 second delay to bash startup,
probably from the loading of Perl.  However, my Herculean efforts to get such
functionality without Perl (i.e. with just shell scripting and cygpath) came to
naught.  I'm guessing an equivalent script might be possible in awk, but that's
an exercise (==challenge) for the reader ;-).

FINE PRINT:
Offer void in CA, AK, WA, and where prohibited by law.  No bailment created.
Not to be used as a flotation device.  Phenylketonurics: Contains phenylalanine.
To be used only for good, never evil.  Contains less than 10% fruit juice.  May
not be suitable for some viewers.  Light fuse and get away.

--
Gary R. Van Sickle
Brewer.  Patriot.


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019