Advertisement
andybuckley

Shell path manipulation functions

Oct 27th, 2013
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.39 KB | None | 0 0
  1. ## Path manipulation functions
  2.  
  3. ## Reprint a colon-separated path with each element on a separate line
  4. function pathsplit {
  5.     # TODO: add customisation of split character
  6.     # TODO: clean first? Configurable?
  7.     case $# in
  8.         0) read i && pathhead "$i";;
  9.         1) echo "$1" | sed 's/:/\n/g';;
  10.         *) echo "Usage: pathsplit <path> or echo <path> | pathsplit"; return 1;;
  11.     esac
  12. }
  13.  
  14.  
  15. ## Print the first element on a colon-separated path
  16. function pathhead {
  17.     case $# in
  18.         0) read i && pathhead "$i";;
  19.         1) echo "$1" | sed -e 's/\([^:]\+\):\?\(.*\)/\1/g';;
  20.         *) echo "Usage: pathhead <path> or echo <path> | pathhead"; return 1;;
  21.     esac
  22. }
  23.  
  24.  
  25. ## Print the trailing elements (if any) on a colon-separated path
  26. function pathtail {
  27.     case $# in
  28.         0) read i && pathtail "$i";;
  29.         1) echo "$1" | sed -e 's/\([^:]\+\):\?\(.*\)/\2/g';;
  30.         *) echo "Usage: pathtail <path> or echo <path> | pathtail"; return 1;;
  31.     esac
  32. }
  33.  
  34.  
  35. ## Remove accidental repeated and leading/trailing colon separators from a path
  36. function pathclean {
  37.     case $# in
  38.         0) read i && pathclean "$i";;
  39.         1) echo "$1" | sed -e 's/:\+/:/g' -e 's/^://g' -e 's/:$//g';;
  40.         *) echo "Usage: pathclean <path> or echo <path> | pathclean"; return 1;;
  41.     esac
  42. }
  43. ## Same function as pathclean, but applied in-place to a named path
  44. function pathcleani {
  45.     case $# in
  46.         1) tmp=$(eval "pathclean \$$1"); eval "$1=$tmp"; unset tmp;;
  47.         *) echo "Usage: pathcleani <varname>"; return 1;;
  48.     esac
  49. }
  50.  
  51.  
  52. ## Reduce a colon-separated path so that each entry appears at most once (at its earliest position)
  53. function pathuniq {
  54.     case $# in
  55.         0) read i && pathclean "$i";;
  56.         1) tmp=`pathclean $1`
  57.             rtn=""
  58.             while true; do
  59.                 head=`pathhead "$tmp"`
  60.                 test -z "$head" && break # Escape if the string is empty
  61.                 tmp=`pathtail "$tmp"`
  62.                 #echo "$head ... $tmp => $rtn" # Debug printout
  63.                 (echo ":$rtn:" | grep ":$head:" &> /dev/null) && continue
  64.                 rtn="$rtn:$head"
  65.             done
  66.             pathclean $rtn
  67.             ;;
  68.         *) echo "Usage: pathuniq <path> or echo <path> | pathuniq"; return 1;;
  69.     esac
  70. }
  71. ## Same function as pathuniq, but applied in-place to a named path
  72. function pathuniqi {
  73.     case $# in
  74.         1) tmp=$(eval "pathuniq \$$1"); eval "$1=$tmp"; unset tmp;;
  75.         *) echo "Usage: pathuniqi <varname>"; return 1;;
  76.     esac
  77. }
  78.  
  79.  
  80. ## Prepend a new path to an existing named path variable
  81. function pathprepend() {
  82.     # TODO: optionally do uniqueness as well as cleaning
  83.     # TODO: make cleaning optional
  84.     case $# in
  85.         2) eval "tmp=$2:\$$1"; pathclean "$tmp";;
  86.         *) echo "Usage: pathprepend <varname> <path_to_add>"; return 1;;
  87.     esac
  88. }
  89. ## Same function as pathprepend, but applied in-place to the named path
  90. function pathprependi() {
  91.     case $# in
  92.         2) tmp=`pathprepend $1 $2`; eval "$1=$tmp";;
  93.         *) echo "Usage: pathprependi <varname> <path_to_add>"; return 1;;
  94.     esac
  95. }
  96.  
  97.  
  98. ## Append a new path to an existing named path variable
  99. function pathappend() {
  100.     # TODO: optionally do uniqueness as well as cleaning
  101.     # TODO: make cleaning optional
  102.     case $# in
  103.         2) eval "tmp=\$$1:$2"; pathclean "$tmp";;
  104.         *) echo "Usage: pathappend <varname> <path_to_add>"; return 1;;
  105.     esac
  106.  }
  107. ## Same function as pathprepend, but applied in-place to the named path
  108. function pathappendi() {
  109.     case $# in
  110.         2) tmp=`pathappend $1 $2`; eval "$1=$tmp";;
  111.         *) echo "Usage: pathappendi <varname> <path_to_add>"; return 1;;
  112.     esac
  113. }
  114.  
  115.  
  116. ## Remove a path element from a colon-separated path
  117. function pathrm() {
  118.     case $# in
  119.         2) tmp=`echo ":$1:" | sed -e "s/:$2:/:/g"`; pathclean $tmp;;
  120.         *) echo "Usage: pathrm <path> <path_to_rm>"; return 1;;
  121.     esac
  122. }
  123. ## Same function as pathrm, but applied in-place to the named path
  124. function pathrmi() {
  125.     case $# in
  126.         2) tmp=$(eval "pathrm \$$1 $2"); eval "$1=$tmp";;
  127.         *) echo "Usage: pathrmi <varname> <path_to_rm>"; return 1;;
  128.     esac
  129. }
  130.  
  131.  
  132. ## List the absolute path to a relatively-specified file/dir
  133. function pathto() {
  134.     case $# in
  135.         0) pwd;;
  136.         1) dir=$(cd `dirname $1` && pwd); pathclean "$dir/`basename $1`";;
  137.         *) echo "Usage: pathto <relpath>"; return 1;;
  138.     esac
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement