Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATH editing in a script



Hank Hughes wrote:
> 
> I made some `quick' hacks to edit my path whilst in hustle-mode.
> The functions are listed below. Are there more correct or efficient
> ways to do any of these?

Hm, how about this (caution, some long lines may be broken up by the
mailer):

--------<snip> --------

paths()                              # Print the index order and the
directory
{ 
    # TODO: find a builtin sequence to replace awk hell
    #       something like `foreach dir ( $path ) { printf() }'?

    usage="Usage: (-i|-n) or (-d|-a) for numeric or alphabetical order."

    typeset     p psort par
    typeset -R5 i=1

    while getopts "inda" par
    do
        case $par in
        (i|n)
            print "Index   Directory"
            print - "----- 
------------------------------------------------------------------------"
            for p in $path
            do
                print "$i   $p"
                (( ++i ))
            done
            ;;
        (d|a)
            print "Index   Directory"
            print - "-----   
------------------------------------------------------------------------"

            for p in ${(o)path}
            do
                i=$path[(ri)$p]
                print "$i   $p"
            done
            ;;
        (*)
            print "$usage" 
        esac
    done
}

addpath()   { path=($path $1) }      # Append to path
prepath()   { path=($1 $path) }      # Prepend to path
rmpath()                             # Remove from path
{                                    # Depending on tab complete ...
    path=(${(R)path:#${1%/}})  
}
replpath()                           # Replace path with path
{ 
    path[$path[(ri)${1%/}]]=${2%/}   # Replace matched index with new
directory
} 

--------<snap> --------

You don't need 'awk'. Zsh can do it all. Note that the way you added a
component to
$path has been wrong. $path is an array, $PATH isn't, so either
path=($path $1)
or PATH=$PATH:$1, but not path=($path:$1)

BTW, for practical use you'd probabely better use an index as parameter
for
rmpath and replpath.

Regards,
	Bernd

--
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@xxxxxxxxxxxxxxxxxx
http://www.rrz.uni-hamburg.de/eggink/BEggink.html



Messages sorted by: Reverse Date, Date, Thread, Author