public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
@ 2006-05-21 22:56 Kevin O'Gorman
  2006-05-23 20:06 ` znx
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin O'Gorman @ 2006-05-21 22:56 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 744 bytes --]

I have inherited some pretty gnarly dotfiles that I don't really want to
fool with too much,
but I'm also unhappy with what they do to my environment.  They keep adding
the
same things over and over to some of the variables.

Does anyone know a nice little idiom for de-duping a colon-list like PATH or
MANPATH?
It has to retain one copy of each duplicate, preserving the order of *first*
appearances.
I know how to avoid duplicates when I do the coding myself:
       case :$PATH: in
             *:mynewthing:*) ;;
             *) export PATH=$PATH:mynewthing
       esac

I'm just not sure how best to turn the colon-list into something I can
iterate over.

Obviously, I use bash.

++ kevin

-- 
Kevin O'Gorman, PhD

[-- Attachment #2: Type: text/html, Size: 1010 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-05-21 22:56 [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow Kevin O'Gorman
@ 2006-05-23 20:06 ` znx
  2006-05-24 14:58   ` Kevin O'Gorman
  0 siblings, 1 reply; 8+ messages in thread
From: znx @ 2006-05-23 20:06 UTC (permalink / raw
  To: gentoo-user

On 21/05/06, Kevin O'Gorman <kogorman@gmail.com> wrote:
>  Does anyone know a nice little idiom for de-duping a colon-list like PATH
> or MANPATH?

Yeah this is something that constantly annoyed me, I forget where I
found this (although I moved it to a function), it is not of my
creation.

To ensure no confusion with the below paste, I have additionally
placed this in a text file:
http://kutzooi.co.uk/cleanpath.sh.txt

function cleanpath {
		# Removes duplicates from PATH style variables
	local variable='PATH'
	if [ $# -eq 1 ]
	then
		variable="$1"
	fi
	local var="${1:-${variable}}" oldpath newpath=: entry
	oldpath="${!var}:"
	while [ -n "$oldpath" ]; do
		entry="${oldpath%%:*}"
		oldpath="${oldpath#*:}"
		[ "${entry:0:1}" = / ] && [ -n "${newpath##*:$entry:*}" ] && \
			[ -d "$entry" ] && newpath="$newpath$entry:"
	done
	newpath="${newpath#:}"
	eval "$var"'="${newpath%:}"'
}

cleanpath					# defaults to PATH
cleanpath MANPATH
cleanpath LD_LIBRARY_PATH

Hope this helps.
Mark

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-05-23 20:06 ` znx
@ 2006-05-24 14:58   ` Kevin O'Gorman
  2006-05-26  1:27     ` znx
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin O'Gorman @ 2006-05-24 14:58 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1597 bytes --]

On 5/23/06, znx <znxster@gmail.com> wrote:
>
> On 21/05/06, Kevin O'Gorman <kogorman@gmail.com> wrote:
> >  Does anyone know a nice little idiom for de-duping a colon-list like
> PATH
> > or MANPATH?
>
> Yeah this is something that constantly annoyed me, I forget where I
> found this (although I moved it to a function), it is not of my
> creation.
>
> To ensure no confusion with the below paste, I have additionally
> placed this in a text file:
> http://kutzooi.co.uk/cleanpath.sh.txt
>
> function cleanpath {
>                 # Removes duplicates from PATH style variables
>         local variable='PATH'
>         if [ $# -eq 1 ]
>         then
>                 variable="$1"
>         fi
>         local var="${1:-${variable}}" oldpath newpath=: entry
>         oldpath="${!var}:"
>         while [ -n "$oldpath" ]; do
>                 entry="${oldpath%%:*}"
>                 oldpath="${oldpath#*:}"
>                 [ "${entry:0:1}" = / ] && [ -n "${newpath##*:$entry:*}" ]
> && \
>                         [ -d "$entry" ] && newpath="$newpath$entry:"
>         done
>         newpath="${newpath#:}"
>         eval "$var"'="${newpath%:}"'
> }
>
> cleanpath                                       # defaults to PATH
> cleanpath MANPATH
> cleanpath LD_LIBRARY_PATH
>
> Hope this helps.
> Mark
>
> --
> gentoo-user@gentoo.org mailing list
>
> This _does_ help.  It's mysterious enough that I tested it, and it seems
to work
except that it removes "." from any path.  This is not quite what I want.

++ kevin


-- 
Kevin O'Gorman, PhD

[-- Attachment #2: Type: text/html, Size: 3346 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-05-24 14:58   ` Kevin O'Gorman
@ 2006-05-26  1:27     ` znx
  2006-05-27  2:52       ` Kevin O'Gorman
  0 siblings, 1 reply; 8+ messages in thread
From: znx @ 2006-05-26  1:27 UTC (permalink / raw
  To: gentoo-user

> This _does_ help.  It's mysterious enough that I tested it, and it seems to
> work except that it removes "." from any path.  This is not quite what I want.

Glad it was almost a success ;) Interesting, thats not something I
noticed before, I have never wished "." in my PATH, I should point out
of course that "." in your PATH is a security risk waiting to happen
;)

Nevertheless it should not be removing the entry unless its a dup. I
have quickly confirmed the behaviour it is defn stripping all "."
entries.

*sigh* the script needs some work .. I'll play with it again tomorrow....

Thanks for the feedback,
Mark

-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-05-26  1:27     ` znx
@ 2006-05-27  2:52       ` Kevin O'Gorman
  2006-06-03 21:11         ` znx
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin O'Gorman @ 2006-05-27  2:52 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1352 bytes --]

On 5/25/06, znx <znxster@gmail.com> wrote:
>
> > This _does_ help.  It's mysterious enough that I tested it, and it seems
> to
> > work except that it removes "." from any path.  This is not quite what I
> want.
>
> Glad it was almost a success ;) Interesting, thats not something I
> noticed before, I have never wished "." in my PATH, I should point out
> of course that "." in your PATH is a security risk waiting to happen
> ;)


Open to debate.  I'd think it's not very dangerous at the *end* of the PATH.
The usual worry is a Trojan masquerading as a normal utility, chosen
early because of the order of things in the searchpath.  When I use ".", I
put it at the end.  It's no worse than the fairly common ~/bin in a
searchpath,
which I'd actually rate more dangerous --
   1) It's just as hard/easy to put something in ~/bin as in ~/
   2) It's more likely to go unnoticed in ~/bin

And, since I do lots of development of itsy-bitsy scripts and C programs,
I'm using such stuff all the time.


Nevertheless it should not be removing the entry unless its a dup. I
> have quickly confirmed the behaviour it is defn stripping all "."
> entries.
>
> *sigh* the script needs some work .. I'll play with it again tomorrow....
>
> Thanks for the feedback,
> Mark


Thanks...

++ kevin

-- 
Kevin O'Gorman, PhD

[-- Attachment #2: Type: text/html, Size: 1953 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-05-27  2:52       ` Kevin O'Gorman
@ 2006-06-03 21:11         ` znx
  2006-07-05 19:33           ` Boyd Stephen Smith Jr.
  0 siblings, 1 reply; 8+ messages in thread
From: znx @ 2006-06-03 21:11 UTC (permalink / raw
  To: gentoo-user

Hi,

First .. arrgghh .. I forgot, I am deeply sorry..

On 27/05/06, Kevin O'Gorman <kogorman@gmail.com> wrote:
>  Open to debate.  I'd think it's not very dangerous at the *end* of the
> PATH.

True, I have modified the script so that a . may enter the PATH (etc)
only as the final entry. Also good point about ~/bin .. it is just as
dangerous.

>  Thanks...

Check out this:
http://kutzooi.co.uk/cleanpath.sh.txt

All dups will be stripped, all "." entries (bar the last) will be stripped also.

Hope this is what you needed.

Mark
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-06-03 21:11         ` znx
@ 2006-07-05 19:33           ` Boyd Stephen Smith Jr.
  2006-07-07 18:31             ` Kevin O'Gorman
  0 siblings, 1 reply; 8+ messages in thread
From: Boyd Stephen Smith Jr. @ 2006-07-05 19:33 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1333 bytes --]

On Saturday 03 June 2006 16:11, znx <znxster@gmail.com> wrote about 'Re: 
[gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and 
grow':
> On 27/05/06, Kevin O'Gorman <kogorman@gmail.com> wrote:
> >  Open to debate.  I'd think it's not very dangerous at the *end* of
> > the PATH.
>
> True, I have modified the script so that a . may enter the PATH (etc)
> only as the final entry. Also good point about ~/bin .. it is just as
> dangerous.

Actually, it's not as dangerous.  ~/bin is a well-known location that is 
(normally) only writable by the user themselves.  '.' is a floating 
location, that may (from time to time) refer to a directory that is 
world-writable like /tmp, /var/tmp, or /dev/shm.

Having '.' in your path allows arbitrary guest users to run programs with 
your permissions.  Putting it at the end of your PATH prevents them from 
shadowing existing commands, but doesn't prevent them from taking 
advantage of typos.

Having ~/bin or even just ~ in your PATH does not open this security hole 
unless you also make that directory world writable.

-- 
"If there's one thing we've established over the years,
it's that the vast majority of our users don't have the slightest
clue what's best for them in terms of package stability."
-- Gentoo Developer Ciaran McCreesh

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow
  2006-07-05 19:33           ` Boyd Stephen Smith Jr.
@ 2006-07-07 18:31             ` Kevin O'Gorman
  0 siblings, 0 replies; 8+ messages in thread
From: Kevin O'Gorman @ 2006-07-07 18:31 UTC (permalink / raw
  To: gentoo-user

On 7/5/06, Boyd Stephen Smith Jr. <bss03@volumehost.net> wrote:
> On Saturday 03 June 2006 16:11, znx <znxster@gmail.com> wrote about 'Re:
> [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and
> grow':
> > On 27/05/06, Kevin O'Gorman <kogorman@gmail.com> wrote:
> > >  Open to debate.  I'd think it's not very dangerous at the *end* of
> > > the PATH.
> >
> > True, I have modified the script so that a . may enter the PATH (etc)
> > only as the final entry. Also good point about ~/bin .. it is just as
> > dangerous.
>
> Actually, it's not as dangerous.  ~/bin is a well-known location that is
> (normally) only writable by the user themselves.  '.' is a floating
> location, that may (from time to time) refer to a directory that is
> world-writable like /tmp, /var/tmp, or /dev/shm.
>
> Having '.' in your path allows arbitrary guest users to run programs with
> your permissions.  Putting it at the end of your PATH prevents them from
> shadowing existing commands, but doesn't prevent them from taking
> advantage of typos.
>
> Having ~/bin or even just ~ in your PATH does not open this security hole
> unless you also make that directory world writable.

Good point.


I've also fooled around with the script a bit, and arrived at something that's
easier for me to read, and a bit more permissive.  YMMV


compresspath ()
{
    local var="${1:-PATH}"   # arg 1; default to $PATH
    local newpath=:
    local entry
    for entry in ${!var//:/ };    # change ":" to space (so separates words)
    do
        case $newpath in
            *:${entry}:*)         # already there -- do nothing
            ;;
            *)
                newpath=$newpath$entry:
            ;;
        esac
    done
    newpath="${newpath#:}";    # drop leading ":"
    newpath="${newpath%:}";   # drop trailing ":"
    eval "$var"'="${newpath}"'
}

++ kevin

-- 
Kevin O'Gorman, PhD
-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-07-07 18:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-21 22:56 [gentoo-user] bash wizardry needed: PATH and MANPATH grow and grow and grow Kevin O'Gorman
2006-05-23 20:06 ` znx
2006-05-24 14:58   ` Kevin O'Gorman
2006-05-26  1:27     ` znx
2006-05-27  2:52       ` Kevin O'Gorman
2006-06-03 21:11         ` znx
2006-07-05 19:33           ` Boyd Stephen Smith Jr.
2006-07-07 18:31             ` Kevin O'Gorman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox