Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions upto-posix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
# shellcheck "$0"
upto() {
# "local" isn't POSIX technically...
local EXPRESSION="$1"
if [ -z "$EXPRESSION" ]; then
echo "A folder expression must be provided." >&2
return 1
fi
if [ "$EXPRESSION" = "/" ]; then
cd "/"
return #0
fi
local CURRENT_FOLDER="$(pwd)" # why not $PWD?
local MATCHED_DIR=""
local MATCHING=true

while [ "$MATCHING" = true ]; do
case "$CURRENT_FOLDER" in
(*${EXPRESSION}*)
MATCHED_DIR="$CURRENT_FOLDER"
CURRENT_FOLDER=$(dirname "$CURRENT_FOLDER") || return
;;
(*)
MATCHING=false ;;
esac
done
if [ -n "$MATCHED_DIR" ]; then
cd "$MATCHED_DIR"
return #0
else
echo "No Match." >&2
return 1
fi
}

# complete upto
#_upto () {
# # necessary locals for _init_completion
# local cur prev words cword
# _init_completion || return
#
# COMPREPLY+=( $( compgen -W "$( echo ${PWD//\// } )" -- $cur ) )
#}
## This complete scheme relies on bash_completion, and the subsequent
## _init_completion function to work.
#declare -f _init_completion > /dev/null && complete -F _upto upto