From 416fc33ff1e9e034cf2bb4a58bb177f46606afd5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 20 Aug 2016 12:29:24 +1200 Subject: Port ud() to POSIX sh --- sh/shrc.d/ud.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sh/shrc.d/ud.sh (limited to 'sh/shrc.d') diff --git a/sh/shrc.d/ud.sh b/sh/shrc.d/ud.sh new file mode 100644 index 00000000..0dfd858c --- /dev/null +++ b/sh/shrc.d/ud.sh @@ -0,0 +1,42 @@ +# Shortcut to step up the directory tree with an arbitrary number of steps, +# like cd .., cd ../.., etc +ud() { + + # Change the positional parameters from the number of steps given to a + # "../../.." string + set -- "$( + + # Check first argument, number of steps upward, default to 1 + # "0" is weird, but valid; "-1" however makes no sense at all + steps=${1:-1} + if [ "$steps" -lt 0 ] ; then + printf >&2 'ud(): Invalid step count\n' + exit 2 + fi + + # Check second argument, target directory, default to $PWD + dirname=${2:-"$PWD"} + + # Append /.. to the target the specified number of times + i=0 + while [ "$i" -lt "$steps" ] ; do + dirname=${dirname%/}/.. + i=$((i+1)) + done + + # Check we have a target after all that + if [ -z "$dirname" ] ; then + printf >&2 'ud(): Destination construction failed\n' + exit 1 + fi + + # Print the target + printf '%s\n' "$dirname" + + # If the subshell failed, return from the function with the same exit + # value + )" || return + + # Try to change into the determined directory + command cd -- "$@" +} -- cgit v1.2.3