diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-20 17:27:04 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-20 17:27:04 +1200 |
commit | 78ca825a27ea02ed17be226f253cecb0e3a71daa (patch) | |
tree | 2ba1c404e97fbde9386c4c1e3926c549d9b72a2b /bash/bashrc.d | |
parent | Port fnl() to POSIX sh script fnl(1) (diff) | |
download | dotfiles-78ca825a27ea02ed17be226f253cecb0e3a71daa.tar.gz dotfiles-78ca825a27ea02ed17be226f253cecb0e3a71daa.zip |
Port path() to POSIX sh
That was a lot easier than I thought
Diffstat (limited to 'bash/bashrc.d')
-rw-r--r-- | bash/bashrc.d/path.bash | 180 |
1 files changed, 0 insertions, 180 deletions
diff --git a/bash/bashrc.d/path.bash b/bash/bashrc.d/path.bash deleted file mode 100644 index 61bf73c0..00000000 --- a/bash/bashrc.d/path.bash +++ /dev/null @@ -1,180 +0,0 @@ -# Function to manage contents of PATH variable within the current shell -path() { - - # Figure out command being called - local pathcmd - if (($#)) ; then - pathcmd=$1 - shift - else - pathcmd=list - fi - - # Switch between commands - case $pathcmd in - - # Print help output (also done if command not found) - help|h|-h|--help|-\?) - while IFS= read -r line ; do - printf '%s\n' "$line" - done <<EOF -$FUNCNAME: Manage contents of PATH variable - -USAGE: - $FUNCNAME h[elp] - Print this help message (also done if command not found) - $FUNCNAME l[ist] - Print the current directories in PATH, one per line (default command) - $FUNCNAME i[nsert] DIR - Add a directory to the front of PATH, checking for existence and uniqueness - $FUNCNAME a[ppend] DIR - Add a directory to the end of PATH, checking for existence and uniqueness - $FUNCNAME r[emove] DIR - Remove all instances of a directory from PATH - -INTERNALS: - $FUNCNAME s[et] [DIR1 [DIR2...]] - Set the PATH to the given directories without checking existence or uniqueness - $FUNCNAME c[heck] DIR - Return whether DIR is a component of PATH - -EOF - ;; - - # Print the current contents of the path - list|l) - local -a patharr - IFS=: read -a patharr < <(printf '%s\n' "$PATH") - if ((${#patharr[@]})) ; then - printf '%s\n' "${patharr[@]}" - fi - ;; - - # 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 dirname - dirname=$1 - [[ $dirname == / ]] || dirname=${dirname%/} - if [[ -z $dirname ]] ; then - printf 'bash: %s: need a directory path to insert\n' \ - "$FUNCNAME" >&2 - return 1 - fi - if [[ ! -d $dirname ]] ; then - printf 'bash: %s: %s not a directory\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - if [[ $dirname == *:* ]] ; then - printf 'bash: %s: Cannot add insert directory %s with colon in name\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - if path check "$dirname" ; then - printf 'bash: %s: %s already in PATH\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - patharr=("$dirname" "${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 dirname - dirname=$1 - [[ $dirname == / ]] || dirname=${dirname%/} - if [[ -z $dirname ]] ; then - printf 'bash: %s: need a directory path to append\n' \ - "$FUNCNAME" >&2 - return 1 - fi - if [[ ! -d $dirname ]] ; then - printf 'bash: %s: %s not a directory\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - if [[ $dirname == *:* ]] ; then - printf 'bash: %s: Cannot append directory %s with colon in name\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - if path check "$dirname" ; then - printf 'bash: %s: %s already in PATH\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - patharr[${#patharr[@]}]=$dirname - 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 dirname - dirname=$1 - [[ $dirname == / ]] || dirname=${dirname%/} - if [[ -z $dirname ]] ; then - printf 'bash: %s: need a directory path to remove\n' \ - "$FUNCNAME" >&2 - return 1 - fi - if ! path check "$dirname" ; then - printf 'bash: %s: %s not in PATH\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - local -a newpatharr - local part - for part in "${patharr[@]}" ; do - [[ $dirname == "$part" ]] && continue - newpatharr[${#newpatharr[@]}]=$part - done - path set "${newpatharr[@]}" - ;; - - # Set the PATH to the given directories without checking existence or uniqueness - set|s) - local -a newpatharr - local dirname - for dirname ; do - newpatharr[${#newpatharr[@]}]=$dirname - done - PATH=$(IFS=: ; printf '%s' "${newpatharr[*]}") - ;; - - # Return whether directory is a component of PATH - check|c) - local -a patharr - IFS=: read -a patharr < <(printf '%s\n' "$PATH") - local dirname - dirname=$1 - [[ $dirname == / ]] || dirname=${dirname%/} - if [[ -z $dirname ]] ; then - printf 'bash: %s: need a directory path to check\n' \ - "$FUNCNAME" >&2 - return 1 - fi - local part - for part in "${patharr[@]}" ; do - if [[ $dirname == "$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 -} |