aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-05-26 20:45:37 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-05-26 20:45:37 +1200
commit0eca7eafffbd667b7e5f4d2423adb54ea5582165 (patch)
tree9b0fe3747d663489338592f342f68352abd58888 /sh/shrc.d
parentRemove hare-brained no-op line (diff)
downloaddotfiles-0eca7eafffbd667b7e5f4d2423adb54ea5582165.tar.gz
dotfiles-0eca7eafffbd667b7e5f4d2423adb54ea5582165.zip
Tidy/golf gt() down a bit
Diffstat (limited to 'sh/shrc.d')
-rw-r--r--sh/shrc.d/gt.sh28
1 files changed, 13 insertions, 15 deletions
diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh
index c90af073..95ab4c2f 100644
--- a/sh/shrc.d/gt.sh
+++ b/sh/shrc.d/gt.sh
@@ -3,26 +3,24 @@
gt() {
# Check argument count
- if [ "$#" -gt 1 ] ; then
- printf >&2 'gt(): Too many arguments\n'
+ if [ "$#" -ne 1 ] ; then
+ printf >&2 'gt(): Need one argument\n'
return 2
fi
- # Strip trailing slash
- set -- "${1%/}"
-
- # If target doesn't have a leading slash, add PWD prefix
- case $1 in
- /*) ;;
- *) set -- "${PWD%/}"/"$1"
- esac
+ # 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
# If target isn't a directory, chop to its parent
[ -d "$1" ] || set -- "${1%/*}"
- # If target is now empty, go to the root
- [ -n "$1" ] || set -- /
-
- # Try to change into the determined directory
- command cd -- "$@"
+ # Try to change into the determined directory, or root if empty
+ command cd -- "${1:-/}"
}