diff options
Diffstat (limited to 'sh/shrc.d/gt.sh')
-rw-r--r-- | sh/shrc.d/gt.sh | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh new file mode 100644 index 00000000..95ab4c2f --- /dev/null +++ b/sh/shrc.d/gt.sh @@ -0,0 +1,26 @@ +# If the argument is a directory, change to it. If it's a file, change to its +# parent. Stands for "get to". +gt() { + + # Check argument count + if [ "$#" -ne 1 ] ; then + printf >&2 'gt(): Need one argument\n' + return 2 + fi + + # 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%/*}" + + # Try to change into the determined directory, or root if empty + command cd -- "${1:-/}" +} |