aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-05-26 20:34:16 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-05-26 20:35:30 +1200
commit62f7ea87871b861a079a64ef0e278370865498e4 (patch)
tree4c493febe27c3137f3232d3075ab2c2b041b31ed /sh/shrc.d
parentReimplement ud() (diff)
downloaddotfiles-62f7ea87871b861a079a64ef0e278370865498e4.tar.gz
dotfiles-62f7ea87871b861a079a64ef0e278370865498e4.zip
Still tinkering with ?d.sh scripts
Diffstat (limited to 'sh/shrc.d')
-rw-r--r--sh/shrc.d/pd.sh49
-rw-r--r--sh/shrc.d/rd.sh2
-rw-r--r--sh/shrc.d/sd.sh2
3 files changed, 20 insertions, 33 deletions
diff --git a/sh/shrc.d/pd.sh b/sh/shrc.d/pd.sh
index ce43837b..e3a6daaa 100644
--- a/sh/shrc.d/pd.sh
+++ b/sh/shrc.d/pd.sh
@@ -2,39 +2,30 @@
# use when you've got a file path in a variable, or in history, or in Alt+.,
# and want to quickly move to its containing directory. In the absence of an
# argument, this just shifts up a directory, i.e. `cd ..`
+#
+# Note this is equivalent to `ud 1`.
pd() {
- # Check argument count
+ # Check arguments; default to $PWD
if [ "$#" -gt 1 ] ; then
printf >&2 'pd(): Too many arguments\n'
return 2
fi
-
- # Change the positional parameters from the target to its containing
- # directory
- set -- "$(
-
- # Figure out target dirname
- dirname=${1:-..}
- dirname=${dirname%/}
- dirname=${dirname%/*}
-
- # Check we have a target after that
- if [ -z "$dirname" ] ; then
- printf >&2 'ud(): Destination construction failed\n'
- exit 1
- fi
-
- # Print the target with trailing slash to work around newline stripping
- printf '%s/' "${dirname%/}"
- )"
-
- # Remove trailing slash
- set -- "${1%/}"
-
- # If the subshell printed nothing, return with failure
- [ -n "$1" ] || return
-
- # Try to change into the determined directory
- command cd -- "$@"
+ set -- "${1:-"$PWD"}"
+
+ # Make certain there are no trailing slashes to foul us up, and anchor path
+ # if relative
+ while : ; do
+ case $1 in
+ */) set -- "${1%/}" ;;
+ /*) break ;;
+ *) set -- "$PWD"/"$1" ;;
+ esac
+ done
+
+ # Strip a path element
+ set -- "${1%/*}"
+
+ # Try to change into the determined directory, or root if empty
+ command cd -- "${1:-/}"
}
diff --git a/sh/shrc.d/rd.sh b/sh/shrc.d/rd.sh
index 3b699c0d..c4c1c063 100644
--- a/sh/shrc.d/rd.sh
+++ b/sh/shrc.d/rd.sh
@@ -1,4 +1,3 @@
-#
# Replace the first instance of the first argument string with the second
# argument string in $PWD, and make that the target of the cd builtin. This is
# to emulate a feature of the `cd` builtin in Zsh that I like, but that I think
@@ -12,7 +11,6 @@
# $ rd usr opt
# $ pwd
# /opt/bin
-#
rd() {
# Check argument count
diff --git a/sh/shrc.d/sd.sh b/sh/shrc.d/sd.sh
index 561a77f4..af82b67e 100644
--- a/sh/shrc.d/sd.sh
+++ b/sh/shrc.d/sd.sh
@@ -1,4 +1,3 @@
-#
# Shortcut to switch to another directory with the same parent, i.e. a sibling
# of the current directory.
#
@@ -31,7 +30,6 @@
# /tmp/tmp.ZSunna5Eup/a
#
# Seems to work for symbolic links.
-#
sd() {
# Check argument count