summaryrefslogtreecommitdiff
path: root/sys/cdadd
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cdadd')
-rwxr-xr-xsys/cdadd39
1 files changed, 39 insertions, 0 deletions
diff --git a/sys/cdadd b/sys/cdadd
new file mode 100755
index 0000000..57f1b69
--- /dev/null
+++ b/sys/cdadd
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Add a cd alias to the $ORGD_CDALIAS_PATH file.
+# Checks the path is valid first before adding it, and that alias less than
+# seven characters as per the requirements.
+# Usage: cdadd alias $HOME/target/directory
+# Pass -p to disable the check for a valid path.
+
+
+dir="$(orgdresolv ORGD_SD_PATH)"
+path="$(orgdresolv ORGD_CDALIAS_PATH)"
+alias="$1"
+target="$2"
+
+# Ensure that $ORGD_CDALIAS_PATH exists
+mkdir -p "$dir"
+touch "$path"
+# Check alias less than eight characters.
+[ ${#alias} -gt 8 ] && \
+ echo "Alias ($alias) must be less than eight characters long." && \
+ exit
+
+# Check $target is a valid path.
+
+if [ ! -d "$target" ] ; then
+ if ! isflag '-p' "$@" ; then
+ echo "Target ($target) is not a valid directory" && \
+ exit
+ fi
+fi
+
+# Check if alias already exists as a cd alias, grep with tab to not match part
+# of existing alias.
+awk ' { print $1 } ' $path | grep -q "$alias " && \
+ echo "The alias ($alias) already exists." && \
+ exit
+
+
+echo "$alias $target" >> $path