From 15847e8b4e4c6b338dcca6f7b022c4ab0cadfd84 Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Sat, 3 Mar 2018 12:00:24 -0600 Subject: [PATCH] .bashrc: Cleanup path helper functions - Remove functions I never use: `pshift`, `punshift` - ppop: Misc fixes - ppush: Support taking in a list of paths to append, and to do directory-exists check before apending. - pinsert: Creating, for pushing an entry on the *beginning* of the list. - puniq: Strip out empty pieces, and use `paste` for smarter way to glue list back together [avoid adding trailing ':' chars]. --- .bashrc | 68 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/.bashrc b/.bashrc index e5d3757..fc357b5 100644 --- a/.bashrc +++ b/.bashrc @@ -357,50 +357,64 @@ push_ssh_cert() { # Usage: pls [] # List path entries of PATH or environment variable . -pls () { eval echo \$${1:-PATH} |tr : '\n'; } - -# Usage: pshift [-n ] [] -# Shift entries off the front of PATH or environment var . -# with the option. Useful: pshift $(pwd) -pshift () { - local n=1 - [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; } - eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'" -} +pls () { eval echo \$${1:-PATH} | tr ':' '\n'; } # Usage: ppop [-n ] [] # Pop entries off the end of PATH or environment variable . ppop () { - local n=1 i=0 + local n=1 [ "$1" = "-n" ] && { n=$2; shift 2; } - while [ $i -lt $n ] - do eval "${1:-PATH}='\${${1:-PATH}%:*}'" - i=$(( i + 1 )) + local i=0 var=${1:-PATH} + local list=$(eval echo \$$var) + while [ $i -lt $n ]; do + list=${list%:*} + i=$(( i + 1 )) done + eval "$var='$list'" } -# Usage: prm [] -# Remove from PATH or environment variable . -prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; } +# Usage: ppush [] +# Push an entry onto the end of PATH or enviroment variable . +ppush () { + local var=${2:-PATH} + local list=$(eval echo \$$var) + local i=0 dir="" + IFS=':' read -a dirs <<< "$1" + for ((i=0; i<${#dirs[@]}; i++)); do + dir=$(eval echo "${dirs[$i]}") + [ ! -d "$dir" ] && continue + [ -n "$list" ] && list="$list:$dir" || list="$dir" + done + eval "$var='$list'" +} -# Usage: punshift [] -# Shift onto the beginning of PATH or environment variable . -punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; } +# Usage: pinsert [] +# Push an entry onto the start of PATH or enviroment variable . +pinsert () { + local var=${2:-PATH} + local list=$(eval echo \$$var) + local i=0 dir="" + IFS=':' read -a dirs <<< "${1}" + for ((i=${#dirs[@]}-1; i>=0; i--)); do + dir=$(eval echo "${dirs[$i]}") + [ ! -d "$dir" ] && continue + [ -n "$list" ] && list="$dir:$list" || list="$dir" + done + eval "$var='$list'" +} -# Usage: ppush [] -ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; } +# Usage: prm [] +# Remove from PATH or environment variable . +prm () { eval "${2:-PATH}='$(pls $2 | grep -v "^$1\$" | tr '\n' ':')'"; } -# Usage: puniq [] +# Usage: puniq [] # Remove duplicate entries from a PATH style value while retaining # the original order. Use PATH if no is given. # # Example: # $ puniq /usr/bin:/usr/local/bin:/usr/bin # /usr/bin:/usr/local/bin -puniq () { - echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n | - cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://' -} +puniq () { echo "$1" | tr ':' '\n' | grep -v "^$" | nl | sort -u -k 2,2 | sort -n | cut -f 2- | paste -s -d ':'; } # --------------------------------------------------------------------------- # USER SHELL ENVIRONMENT -- 2.47.1