diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-23 14:28:18 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-23 14:28:18 +1200 |
commit | 1e94aa430c0eefce9396e993272309788c6a21d0 (patch) | |
tree | 7445e71f88a44f2df461a66cd88cd5f5ee14b405 /sh/shrc.d/ud.sh | |
parent | Correct stderr redirection for keychain(1) check (diff) | |
download | dotfiles-1e94aa430c0eefce9396e993272309788c6a21d0.tar.gz dotfiles-1e94aa430c0eefce9396e993272309788c6a21d0.zip |
Fix up ?d() functions
Count arguments in right places; return 2 on usage errors where
possible; minimise subshell activity; move directory replacement
functionality to its own function `rd()` rather than overloading `cd`.
Diffstat (limited to 'sh/shrc.d/ud.sh')
-rw-r--r-- | sh/shrc.d/ud.sh | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/sh/shrc.d/ud.sh b/sh/shrc.d/ud.sh index 259f3167..44a3a81d 100644 --- a/sh/shrc.d/ud.sh +++ b/sh/shrc.d/ud.sh @@ -2,23 +2,27 @@ # like cd .., cd ../.., etc ud() { + # Check argument count + if [ "$#" -gt 1 ] ; then + printf >&2 'ud(): Too many arguments\n' + return 2 + fi + + # Check first argument, number of steps upward, default to 1. + # "0" is weird, but valid; "-1" however makes no sense at all + if [ "${1:-1}" -lt 0 ] ; then + printf >&2 'ud(): Invalid step count\n' + return 2 + fi + # 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 + # Append /.. to the target (default PWD) the specified number of times dirname=${2:-"$PWD"} - - # Append /.. to the target the specified number of times i=0 + steps=${1:-1} while [ "$i" -lt "$steps" ] ; do dirname=${dirname%/}/.. i=$((i+1)) |