diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-06-05 10:50:37 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-06-05 10:50:37 +1200 |
commit | 8043bb1a97c2e36b33be50aa2152d961d2d2885e (patch) | |
tree | 276804572e1d40b4ed99aea67b97c2ec28d6e28d | |
parent | Workaround to give han(1) help pages better names (diff) | |
download | dotfiles-8043bb1a97c2e36b33be50aa2152d961d2d2885e.tar.gz dotfiles-8043bb1a97c2e36b33be50aa2152d961d2d2885e.zip |
Add path() function
-rw-r--r-- | README.markdown | 1 | ||||
-rw-r--r-- | bash/bashrc.d/path.bash | 158 |
2 files changed, 159 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index 51959ae7..e55ade14 100644 --- a/README.markdown +++ b/README.markdown @@ -178,6 +178,7 @@ There are a few other little tricks in `bash/bashrc.d`, including: * `mkcp` — Create a directory and copy arguments into it * `mkmv` — Create a directory and move arguments into it * `pa` — Print given arguments, one per line +* `path` — Manage the contents of PATH conveniently * `pd` — Change to the argument’s parent directory * `scr` — Create a temporary directory and change into it * `sprunge` — Pastebin frontend tool I pilfered from `#bash` on Freenode diff --git a/bash/bashrc.d/path.bash b/bash/bashrc.d/path.bash new file mode 100644 index 00000000..b182c739 --- /dev/null +++ b/bash/bashrc.d/path.bash @@ -0,0 +1,158 @@ +# Function to manage contents of PATH variable within the current shell +path() { + + # Figure out command being called + local pathcmd=list + if (($#)) ; then + pathcmd=$1 + shift + fi + + # Switch between commands + case $pathcmd in + + # Print help output (also done if command not found) + help|h|-h|--help|-\?) + printf '%s: Manage contents of PATH variable\n' "$FUNCNAME" + printf '\n' + printf 'USAGE:\n' + printf ' %s h[elp]\n' "$FUNCNAME" + printf ' Print this help message (also done if command not found)\n' + printf ' %s l[ist]\n' "$FUNCNAME" + printf ' Print the current dirs in PATH, one per line (default command)\n' + printf ' %s i[nsert] DIR\n' "$FUNCNAME" + printf ' Add a directory to the front of PATH, checking for existence and uniqueness\n' + printf ' %s a[ppend] DIR\n' "$FUNCNAME" + printf ' Add a directory to the end of PATH, checking for existence and uniqueness\n' + printf ' %s r[emove] DIR\n' "$FUNCNAME" + printf ' Remove all instances of a directory from PATH\n' + printf '\n' + printf 'INTERNALS:\n' + printf ' %s s[et] [DIR1 [DIR2...]]\n' "$FUNCNAME" + printf ' Set the PATH to the given directories without checking existence or uniqueness\n' + printf ' %s c[heck] DIR\n' "$FUNCNAME" + printf ' Return whether DIR is a component of PATH\n' + printf '\n' + ;; + + # Print the current contents of the path + list|l) + local -a patharr + IFS=: read -a patharr < <(printf '%s\n' "$PATH") + printf '%s\n' "${patharr[@]}" + ;; + + # Add a directory to the front of PATH, checking for existence and uniqueness + insert|i) + local -a patharr + IFS=: read -a patharr < <(printf '%s\n' "$PATH") + local dir=${1:?} + if [[ -z "$dir" ]] ; then + printf 'bash: %s: need a directory path to insert\n' \ + "$FUNCNAME" >&2 + return 1 + fi + if [[ ! -d $dir ]] ; then + printf 'bash: %s: %s not a directory\n' \ + "$FUNCNAME" "$dir" >&2 + return 1 + fi + if path check "$dir" ; then + printf 'bash: %s: %s already in PATH\n' \ + "$FUNCNAME" "$dir" >&2 + return 1 + fi + patharr=("$dir" "${patharr[@]}") + path set "${patharr[@]}" + ;; + + # Add a directory to the end of PATH, checking for existence and uniqueness + append|add|a) + local -a patharr + IFS=: read -a patharr < <(printf '%s\n' "$PATH") + local dir=$1 + if [[ -z "$dir" ]] ; then + printf 'bash: %s: need a directory path to append\n' \ + "$FUNCNAME" >&2 + return 1 + fi + if [[ ! -d $dir ]] ; then + printf 'bash: %s: %s not a directory\n' \ + "$FUNCNAME" "$dir" >&2 + return 1 + fi + if path check "$dir" ; then + printf 'bash: %s: %s already in PATH\n' \ + "$FUNCNAME" "$dir" >&2 + return 1 + fi + patharr=("${patharr[@]}" "$dir") + path set "${patharr[@]}" + ;; + + # Remove all instances of a directory from PATH + remove|rm|r) + local -a patharr + IFS=: read -a patharr < <(printf '%s\n' "$PATH") + local dir=$1 + if [[ -z "$dir" ]] ; then + printf 'bash: %s: need a directory path to remove\n' \ + "$FUNCNAME" >&2 + return 1 + fi + if ! path check "$dir" ; then + printf 'bash: %s: %s not in PATH\n' \ + "$FUNCNAME" "$dir" >&2 + return 1 + fi + local -a newpatharr + local part + for part in "${patharr[@]}" ; do + if [[ ${dir%/} != "${part%/}" ]] ; then + newpatharr=("${newpatharr[@]}" "$part") + fi + done + path set "${newpatharr[@]}" + ;; + + # Set the PATH to the given directories without checking existence or uniqueness + set|s) + local -a newpatharr + local part + for part in "$@" ; do + newpatharr=("${newpatharr[@]}" "${part%/}") + done + local IFS=: + PATH="${newpatharr[*]}" + ;; + + # Return whether DIR is a component of PATH + check|c) + local -a patharr + IFS=: read -a patharr < <(printf '%s\n' "$PATH") + local dir=${1:?} + if [[ -z "$dir" ]] ; then + printf 'bash: %s: need a directory path to check\n' \ + "$FUNCNAME" >&2 + return 1 + fi + local part + for part in "${patharr[@]}" ; do + if [[ ${dir%/} == "${part%/}" ]] ; then + return 0 + fi + done + return 1 + ;; + + # Unknown command + *) + printf 'bash: %s: Unknown command %s\n' \ + "$FUNCNAME" "$pathcmd" >&2 + path help >&2 + return 1 + ;; + esac +} +complete -W 'help list insert append remove set check' path + |