aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/path.sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-20 17:27:04 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-20 17:27:04 +1200
commit78ca825a27ea02ed17be226f253cecb0e3a71daa (patch)
tree2ba1c404e97fbde9386c4c1e3926c549d9b72a2b /sh/shrc.d/path.sh
parentPort fnl() to POSIX sh script fnl(1) (diff)
downloaddotfiles-78ca825a27ea02ed17be226f253cecb0e3a71daa.tar.gz
dotfiles-78ca825a27ea02ed17be226f253cecb0e3a71daa.zip
Port path() to POSIX sh
That was a lot easier than I thought
Diffstat (limited to 'sh/shrc.d/path.sh')
-rw-r--r--sh/shrc.d/path.sh93
1 files changed, 93 insertions, 0 deletions
diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh
new file mode 100644
index 00000000..22374310
--- /dev/null
+++ b/sh/shrc.d/path.sh
@@ -0,0 +1,93 @@
+# Function to manage contents of PATH variable within the current shell
+path() {
+
+ # The second argument, the directory, can never have a colon
+ case $2 in
+ *:*)
+ printf >&2 'path(): Illegal colon in given directory\n'
+ return 2
+ ;;
+ esac
+
+ # Check first argument to figure out operation
+ case $1 in
+
+ # List current directories in $PATH
+ list|'') (
+ path=$PATH:
+ while [ -n "$path" ] ; do
+ dir=${path%%:*}
+ path=${path#*:}
+ [ -n "$dir" ] || continue
+ printf '%s\n' "$dir"
+ done
+ ) ;;
+
+ # Add a directory at the start of $PATH
+ insert)
+ if path check "$2" ; then
+ printf >&2 'path(): %s already in $PATH\n'
+ return 1
+ fi
+ PATH=${2}${PATH:+:"$PATH"}
+ ;;
+
+ # Add a directory to the end of $PATH
+ append)
+ if path check "$2" ; then
+ printf >&2 'path(): %s already in $PATH\n'
+ return 1
+ fi
+ PATH=${PATH:+"$PATH":}${2}
+ ;;
+
+ # Remove a directory from $PATH
+ remove)
+ if ! path check "$2" ; then
+ printf >&2 'path(): %s not in $PATH\n'
+ return 1
+ fi
+ PATH=$(
+ path=:$path:
+ path=${path%%:"$2":*}:${path#*:"$2":}
+ path=${path#:}
+ path=${path%:}
+ printf '%s\n' "$path"
+ )
+ ;;
+
+ # Check whether a directory is in $PATH
+ check) (
+ path=:$PATH:
+ [ "$path" != "${path%:"$2":*}" ]
+ ) ;;
+
+ # Print help output (also done if command not found)
+ help)
+ cat <<'EOF'
+path(): Manage contents of PATH variable
+
+USAGE:
+ path [list]
+ Print the current directories in PATH, one per line (default command)
+ path insert DIR
+ Add a directory to the front of PATH
+ path append DIR
+ Add a directory to the end of PATH
+ path remove DIR
+ Remove directory from PATH
+ path check DIR
+ Return whether DIR is a component of PATH
+ path help
+ Print this help message (also done if command not found)
+EOF
+ ;;
+
+ # Command not found
+ *)
+ printf >&2 'path(): Unknown command\n'
+ path help
+ return 2
+ ;;
+ esac
+}