How to keep from duplicating path variable in csh

前端 未结 12 933
我在风中等你
我在风中等你 2020-11-27 05:19

It is typical to have something like this in your cshrc file for setting the path:

set path = ( . $otherpath $path )

but, the path gets dup

12条回答
  •  清歌不尽
    2020-11-27 06:15

    I've been using the following (Bourne/Korn/POSIX/Bash) script for most of a decade:

    :   "@(#)$Id: clnpath.sh,v 1.6 1999/06/08 23:34:07 jleffler Exp $"
    #
    #   Print minimal version of $PATH, possibly removing some items
    
    case $# in
    0)  chop=""; path=${PATH:?};;
    1)  chop=""; path=$1;;
    2)  chop=$2; path=$1;;
    *)  echo "Usage: `basename $0 .sh` [$PATH [remove:list]]" >&2
        exit 1;;
    esac
    
    # Beware of the quotes in the assignment to chop!
    echo "$path" |
    ${AWK:-awk} -F: '#
    BEGIN   {   # Sort out which path components to omit
                chop="'"$chop"'";
                if (chop != "") nr = split(chop, remove); else nr = 0;
                for (i = 1; i <= nr; i++)
                    omit[remove[i]] = 1;
            }
    {
        for (i = 1; i <= NF; i++)
        {
            x=$i;
            if (x == "") x = ".";
            if (omit[x] == 0 && path[x]++ == 0)
            {
                output = output pad x;
                pad = ":";
            }
        }
        print output;
    }'
    

    In Korn shell, I use:

    export PATH=$(clnpath /new/bin:/other/bin:$PATH /old/bin:/extra/bin)
    

    This leaves me with PATH containing the new and other bin directories at the front, plus one copy of each directory name in the main path value, except that the old and extra bin directories have bin removed.

    You would have to adapt this to C shell (sorry - but I'm a great believer in the truths enunciated at C Shell Programming Considered Harmful). Primarily, you won't have to fiddle with the colon separator, so life is actually easier.

提交回复
热议问题