Skip to content

Instantly share code, notes, and snippets.

@psaxton
Last active January 6, 2022 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaxton/6937fec3357f8295828d2e3577f9bcb6 to your computer and use it in GitHub Desktop.
Save psaxton/6937fec3357f8295828d2e3577f9bcb6 to your computer and use it in GitHub Desktop.
sh function for finding the first existence of a file in a parent directory. Usage: `lsup [path] file`
lsup() {
if [ $# -eq 1 ]; then
local file=${1} path=$(pwd)
elif [ $# -eq 2 ]; then
local file=${2} path=$(realpath -es "${1}")
else
echo >&2 "Usage: lsup [path] filename"
echo >&2 " At least filename is required"
return 1
fi
[ -z "${SH_DEBUG-}" ] || echo >&2 "Searching for ${file} from ${path}…"
until [ -e "${path}/${file}" ]; do
[ -z "${SH_DEBUG-}" ] || echo >&2 "Not found in ${path}"
[ -n "$path" ] || return 1;
path=${path%/*}
done
echo "${path}/${file}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment