Eric Roode on 11 Jan 2005 14:44:55 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] PATH problem solved


This isn't really relevant to Adam's problem, but it seemed like an opportune
time to share this utility.

I wrote this shell function a long time ago, and I find it very handy.  Perhaps
others will find it useful too.  The function is called 'path'.  It is used to
display and modify the PATH variable.  Usage:

    `path`  displays the PATH
    `path :`  displays the PATH, with each colon-separated component on a
separate line.
    `path + dir`  adds <dir> to the list of paths in PATH
    `path - dir`  removes <dir> from the list of paths in PATH

I have only tested it in the bash shell, but I have used it on Solaris, linux,
and cygwin (and it handles spaces in PATH components that you often find in
Windows).

Enjoy.

# function to add/subtract paths from PATH
function path
{
    if [ "$#" = 0 ]; then
        echo $PATH;
    else
        arg="$1"; shift
        case "$arg" in
        +)      # add to path
                for p in $@; do PATH="$PATH${PATH:+:}$p"; done
                ;;

        -)      # remove from path
                IFS=:
                nv=""
                for p in $PATH; do
                    there=""
                    for a in "$@"; do
                        [ ":$p:" = ":$a:" ]  &&  there=1
                    done

                    if [ -z "$there" ]; then
                        nv="$nv${nv:+:}$p"
                    fi
                done
                PATH="$nv"
                unset IFS
                ;;

        =)      # Set path
                IFS=:
                PATH="$*"
                unset IFS
                ;;

        :)      # Display, one component per line
                cleanup_var PATH
                IFS=:
                for p in $PATH; do
                    echo "$p"
                done
                unset IFS
                ;;

        *)      echo Usage: path [+-=:] path ...;;

        esac

        cleanup_var PATH;
        export PATH
    fi
}

# Remove dups, blanks from a path variable
function cleanup_var
{
    VAR="$1"
    IFS=:
    nv=""
    for p in `echo "${!VAR}" | /bin/tr ":" "\n" | /bin/sort | /bin/uniq | tr
"\n" ":"`; do
        if [ ! -z "$p" ]; then
            # echo "nv=$nv$colon$p"
            nv="$nv${nv:+:}$p"
        fi
    done
    export $VAR="$nv"
    unset IFS
}


Oh, and one final thing.  I have an 'ldpath' function too, that does the same
thing for the LDPATH variable.  It's basically a cut-and-paste of the 'path'
function, with the variable name changed.

-- 
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`


___________________________________________________________________

Privacy Notice: This message has been sent via www.zoemail.net using
patented e-mail protection technology developed by AT&T Labs. Reply to
the "keyed address" above to ensure delivery.

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug