Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Path manipulation functions
- ## Reprint a colon-separated path with each element on a separate line
- function pathsplit {
- # TODO: add customisation of split character
- # TODO: clean first? Configurable?
- case $# in
- 0) read i && pathhead "$i";;
- 1) echo "$1" | sed 's/:/\n/g';;
- *) echo "Usage: pathsplit <path> or echo <path> | pathsplit"; return 1;;
- esac
- }
- ## Print the first element on a colon-separated path
- function pathhead {
- case $# in
- 0) read i && pathhead "$i";;
- 1) echo "$1" | sed -e 's/\([^:]\+\):\?\(.*\)/\1/g';;
- *) echo "Usage: pathhead <path> or echo <path> | pathhead"; return 1;;
- esac
- }
- ## Print the trailing elements (if any) on a colon-separated path
- function pathtail {
- case $# in
- 0) read i && pathtail "$i";;
- 1) echo "$1" | sed -e 's/\([^:]\+\):\?\(.*\)/\2/g';;
- *) echo "Usage: pathtail <path> or echo <path> | pathtail"; return 1;;
- esac
- }
- ## Remove accidental repeated and leading/trailing colon separators from a path
- function pathclean {
- case $# in
- 0) read i && pathclean "$i";;
- 1) echo "$1" | sed -e 's/:\+/:/g' -e 's/^://g' -e 's/:$//g';;
- *) echo "Usage: pathclean <path> or echo <path> | pathclean"; return 1;;
- esac
- }
- ## Same function as pathclean, but applied in-place to a named path
- function pathcleani {
- case $# in
- 1) tmp=$(eval "pathclean \$$1"); eval "$1=$tmp"; unset tmp;;
- *) echo "Usage: pathcleani <varname>"; return 1;;
- esac
- }
- ## Reduce a colon-separated path so that each entry appears at most once (at its earliest position)
- function pathuniq {
- case $# in
- 0) read i && pathclean "$i";;
- 1) tmp=`pathclean $1`
- rtn=""
- while true; do
- head=`pathhead "$tmp"`
- test -z "$head" && break # Escape if the string is empty
- tmp=`pathtail "$tmp"`
- #echo "$head ... $tmp => $rtn" # Debug printout
- (echo ":$rtn:" | grep ":$head:" &> /dev/null) && continue
- rtn="$rtn:$head"
- done
- pathclean $rtn
- ;;
- *) echo "Usage: pathuniq <path> or echo <path> | pathuniq"; return 1;;
- esac
- }
- ## Same function as pathuniq, but applied in-place to a named path
- function pathuniqi {
- case $# in
- 1) tmp=$(eval "pathuniq \$$1"); eval "$1=$tmp"; unset tmp;;
- *) echo "Usage: pathuniqi <varname>"; return 1;;
- esac
- }
- ## Prepend a new path to an existing named path variable
- function pathprepend() {
- # TODO: optionally do uniqueness as well as cleaning
- # TODO: make cleaning optional
- case $# in
- 2) eval "tmp=$2:\$$1"; pathclean "$tmp";;
- *) echo "Usage: pathprepend <varname> <path_to_add>"; return 1;;
- esac
- }
- ## Same function as pathprepend, but applied in-place to the named path
- function pathprependi() {
- case $# in
- 2) tmp=`pathprepend $1 $2`; eval "$1=$tmp";;
- *) echo "Usage: pathprependi <varname> <path_to_add>"; return 1;;
- esac
- }
- ## Append a new path to an existing named path variable
- function pathappend() {
- # TODO: optionally do uniqueness as well as cleaning
- # TODO: make cleaning optional
- case $# in
- 2) eval "tmp=\$$1:$2"; pathclean "$tmp";;
- *) echo "Usage: pathappend <varname> <path_to_add>"; return 1;;
- esac
- }
- ## Same function as pathprepend, but applied in-place to the named path
- function pathappendi() {
- case $# in
- 2) tmp=`pathappend $1 $2`; eval "$1=$tmp";;
- *) echo "Usage: pathappendi <varname> <path_to_add>"; return 1;;
- esac
- }
- ## Remove a path element from a colon-separated path
- function pathrm() {
- case $# in
- 2) tmp=`echo ":$1:" | sed -e "s/:$2:/:/g"`; pathclean $tmp;;
- *) echo "Usage: pathrm <path> <path_to_rm>"; return 1;;
- esac
- }
- ## Same function as pathrm, but applied in-place to the named path
- function pathrmi() {
- case $# in
- 2) tmp=$(eval "pathrm \$$1 $2"); eval "$1=$tmp";;
- *) echo "Usage: pathrmi <varname> <path_to_rm>"; return 1;;
- esac
- }
- ## List the absolute path to a relatively-specified file/dir
- function pathto() {
- case $# in
- 0) pwd;;
- 1) dir=$(cd `dirname $1` && pwd); pathclean "$dir/`basename $1`";;
- *) echo "Usage: pathto <relpath>"; return 1;;
- esac
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement