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

Re: PATH editing in a script



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?

----------------------------------------------------------------------
#
# PATH adjustors
#
# Concepts originally from (Well, at least the first time *I* saw them)
#    Remy Evard <evard@xxxxxxxxxxx>
#
# Re-Inspired by parameter expansions given by 
#    Bernd Eggink <eggink@xxxxxxxxxxxxxxxxxx>
#

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."

    if [ $1 ] 
    then 
	if ( [ $1 = "-i" ] || [ $1 = "-n" ] ) 
	then                         # Sort numerically by index
	    echo "Index   Directory"
	    echo "-----   ------------------------------------------------------------------------"
	    echo $PATH | awk -F: '{ for (i = 1; i <= NF; i++) printf("  %3d    %s\n", ++num, $i)}' | sort
	elif ( [ $1 = "-d" ] || [ $1 = "-a" ] )
        then                         # Sort alphabetically by directory
	    echo "Index   Directory"
	    echo "-----   ------------------------------------------------------------------------"
	    echo $PATH | awk -F: '{ for (i = 1; i <= NF; i++) printf("  %3d    %s\n", ++num, $i)}' | sort +1
	else
	    echo $usage              # Maybe default by index instead?
	fi
    else
        echo $usage
    fi
}

addpath()   { path=($path:$1) }      # Append to path
prepath()   { path=($1:$path) }      # Prepend to path
rmpath()                             # Remove from path
{                                       # Depending on tab complete ...
    dir=`echo $1 | sed 's/\/$//'`       #   Remove finicky trailing slash 
    path=(${(R)path:#$dir})             # Param expansion ...
				        #   ${name:#patt} replace with null
}
replpath()                           # Replace path with path
{ 
    dir1=`echo $1 | sed 's/\/$//'`      # The trailing slashes ..
    dir2=`echo $2 | sed 's/\/$//'`
    path[$path[(ri)$dir1]]=$dir2        # Replace matched index with new directory
} 

#nognu()   { rmpath /arch/gnu/bin }     # Zzzot gnu!



----------------------------------------------------------------------



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