From ece8b924343c45cbb68c93917742dab523750975 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 29 Mar 2017 10:58:42 +1300 Subject: Add lgt() --- README.markdown | 1 + sh/shrc.d/lgt.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 sh/shrc.d/lgt.sh diff --git a/README.markdown b/README.markdown index 874e0fc2..43dddf0c 100644 --- a/README.markdown +++ b/README.markdown @@ -185,6 +185,7 @@ in `sh/shrc.d` to be loaded by any POSIX interactive shell. Those include: `/usr/local/bin`, as long as they are unique. * `bd()` changes into a named ancestor of the current directory. * `gt()` changes into a directory or into a file's directory. + * `lgt()` runs `gt()` on the first result from a `loc(1df)` search. * `mkcd()` creates a directory and changes into it. * `pd()` changes to the argument's parent directory. * `rd()` replaces the first instance of its first argument with its diff --git a/sh/shrc.d/lgt.sh b/sh/shrc.d/lgt.sh new file mode 100644 index 00000000..fbe43369 --- /dev/null +++ b/sh/shrc.d/lgt.sh @@ -0,0 +1,28 @@ +# Run loc(1df) with given arguments and then run gt() to get to the first +# argument found +lgt() { + + # Check argument count + if [ "$#" -eq 0 ] ; then + printf >&2 'lgt(): Need a search term\n' + return 2 + fi + + # Change the positional parameters from the loc(1df) arguments to the first + # result with a trailing slash + set -- "$( + loc "$@" | { + IFS= read -r target + printf '%s/' "$target" + } + )" + + # Strip the trailing slash + set -- "${1%/}" + + # If the subshell printed nothing, return with failure + [ -n "$1" ] || return + + # Run gt() with the new arguments + gt "$@" +} -- cgit v1.2.3