Last active
January 6, 2022 23:06
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lsup() { | |
path="$1"; | |
file="$2"; | |
if [ -z "${file}" ]; then | |
file="${path}"; | |
path="$(pwd)"; | |
fi; | |
path="$(realpath -es "${path}")" | |
[ -n "${SH_DEBUG}" ] && echo "Searching for ${file} from ${path}…"; | |
while [ "${path}" != "/" ]; do | |
[ -e "${path}/${file}" ] && break; | |
[ -n "${SH_DEBUG}" ] && echo "Not found in ${path}"; | |
path="$(realpath -s "${path}/..")"; | |
done; | |
[ -e "${path}/${file}" ] && echo "${path}/${file}" || return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment