From 162f108b44470d34d2dec149b295c6d2a8356d6d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 29 Mar 2017 10:50:26 +1300 Subject: Add gt() (go to) --- sh/shrc.d/gt.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sh/shrc.d/gt.sh (limited to 'sh') diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh new file mode 100644 index 00000000..d18a4ab8 --- /dev/null +++ b/sh/shrc.d/gt.sh @@ -0,0 +1,28 @@ +# 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 [ "$#" -gt 1 ] ; then + printf >&2 'gd(): Too many arguments\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 + + # 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 -- "$@" +} -- cgit v1.2.3 From ece8b924343c45cbb68c93917742dab523750975 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 29 Mar 2017 10:58:42 +1300 Subject: Add lgt() --- sh/shrc.d/lgt.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sh/shrc.d/lgt.sh (limited to 'sh') 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 From 91aa4123f4c90b0176682a80e9dfe7d9b90bf553 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 29 Mar 2017 15:19:44 +1300 Subject: Remove SC2030 ignore for path logic Can't find where this was fixed --- sh/shrc.d/path.sh | 1 - 1 file changed, 1 deletion(-) (limited to 'sh') diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh index 79b48fd5..b6b1820f 100644 --- a/sh/shrc.d/path.sh +++ b/sh/shrc.d/path.sh @@ -6,7 +6,6 @@ path() { # List current directories in PATH list|'') ( - # shellcheck disable=SC2030 path=$PATH: while [ -n "$path" ] ; do dir=${path%%:*} -- cgit v1.2.3