Mike Marion wrote:
> 
> Nah.. it's what we get due to everyone doing:
> set path=( $path <some paths> . )
> all the time.. yes, they just about _all_ use . in their paths.. even after 
> we
> warn them.  This was why one of the past sysadmins wrote a program called
> envmgr ages ago... cool little program that removes duplicates in env 
> variables
> and prints out the syntax you need to set a variable.
> 
> i.e.
> rs-workstation linux {553}$ envmgr -a PATH /bin
> PATH=/pkg/qct/sunray/bin:/usr/bin:/usr/ucb:/usr/afsws/bin:/usr/openwin/bin:/usr/dt/bin:/bin:/usr/local/bin:/usr2/mmarion/bin:/usr/local/sbin:/usr/sbin:/sbin:/pkg/icetools/bin:/usr2/mmarion/pkg/bin:/pkg/ice/sysadmin/bin:/opt/gnome/bin:/usr/X11R6/bin:/pkg/qct/software/bin:/pkg/qct/software/perl/bin;
> export PATH
> 
> with swithces like -a to add, -p to prepend, -r to remove.. etc.  you 
> just call
> it with:
> eval `envmgr <stuff>`
> and it helps keep things clean.  I got so used to it that I use it at 
> home too.
> One of these days I'll see if he ever released it, or if we can.. nothing
> QC-oriented in it AFAIK.  Of course, one could script up something similar
> anyway.
> 
> We try to use envmgr in any scripts we write (and for things like package
> subscriptions.. don't ask) to keep people's env's as clean as we can.

How about this one (see attachement, which mailman had better not
remove!)

-john
#!/bin/sh
# vim:ts=4:sw=4:syn=sh
# environment manager
# based upon an idea presented by Mike Marion
# (C) 2005 John H. Robinson, IV <[EMAIL PROTECTED]>
# Released under the GNU GPL Version 2, and no other.
# $Id: envmgr,v 1.4 2005/03/03 08:19:11 jaqque Exp jaqque $

# BUGS: very difficult (impossible? to include a -a, -r, or -p element
#       you cannot set the env. var Revision or ___a, or a host of other
#               ``reserved'' env. vars.

# TODO: if there is no env var supplied, default to using $PATH
#               i cannot seem to do that in pure sh. if i look in the output of 
env,
#               yes. if i use bash or zsh, yes. pure sh? not that i have found, 
yet

___shell=sh; # if you want csh syntax, first ensure the csh() acts correctly,
          # then add a test that sets $shell to csh for when the current shell
                  # is a csh variant. I don't use lesser shells.

___action=append
___magic='P&C_vvWKf2?Dq`&rx0O-k;GOTczBJa9y' # magic value to determine if an
        # env var is unset or not. can you think of a better test? please let me
        # know. for the curious, it came from the output of pwgen -s 32.

# the following two lines make -v work nicely
Revision=Version 
unset ___a

sh() {
        echo $___var='"'"$1"'"'
}
csh() {
        echo setenv $___var '"'$1'"'
}

#debug information. pretty standard stuff, actually
D() {
        if [ "$DEBUG" ]; then
                echo "$@"
        fi
}

dupes() {
        D dupes "$@"

        unset ___dupe
        unset ___V2
        ___ifs="$IFS"
        IFS=:

        for ___v in $___VAR; do
                D working on $___v in $___VAR
                for ___w in $___V2; do
                        D $___v match $___w ?
                        if [ ".$___v" = ".$___w" ]; then
                                ___dupe=1
                                D skipping $___v
                                break;
                        fi
                done
                if [ ! "$___dupe" ]; then
                        D adding $___v
                        if [ "$___V2" ]; then
                                ___V2="$___V2:$___v"
                        else
                                ___V2="$___v"
                        fi
                fi
                unset ___dupe
        done
        IFS="$___ifs"           

        ___VAR=$___V2
}

usage() {
        cat << EOF
$0 - Environmental variable manager

Usage: $0 VAR [ [-a|-p|-r] <element> ] | [-h|-v]
        -a      appends <element> to \$VAR (default)
        -p      prepends path element> to \$VAR
        -r      removes <element> from \$VAR
        -h      this help
        -v      version
EOF
}

version() {
        cat << EOF
$0 $Revision: 1.4 $___a
Copyright 2005 John H. Robinson, IV <[EMAIL PROTECTED]>
Released under the GNU General Public License version 2, and no other.
EOF
}

remove() {
        D remove "$@"

        unset ___V2
        ___ifs="$IFS"
        IFS=:
        
        for ___v in $___VAR; do
                if [ ! ".$1" = ".$___v" ]; then
                        D adding $___v
                        if [ "$___V2" ]; then
                                ___V2="$___V2:$___v"
                        else
                                ___V2="$___v"
                        fi
                        D $___V2
                else
                        D "removing (skipping) $___v"
                fi
        done    

        IFS=$___ifs
        ___VAR="$___V2"
}

append() {
        D append "$@"
        if [ .$___VAR = . ]; then
                ___VAR=$1
        else
                ___VAR=$___VAR:$1
        fi
}

prepend() {
        D prepend "$@"
        if [ .$___VAR = . ]; then
                ___VAR=$1
        else
                ___VAR=$1:$___VAR
        fi
}

case $1 in
        -v)     version
                ;;
        -h)     usage
                ;;
        *)      ___var=$1
                ___VAR=`eval echo \\$$___var`
                D $___var=$___VAR
                ;;
esac

shift

while [ ! ."$1" = . ]; do
        D Main Loop: $1
        case $1 in
                -p) ___action=prepend
                        ;;
                -r) ___action=remove
                        ;;
                -a) ___action=append
                        ;;
                *) $___action "$1"
                        ;;
        esac
        shift
done

dupes "$___VAR"
$___shell $___VAR
-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to