Last active
September 19, 2018 23:26
-
-
Save paxperscientiam/c6aea78f06f82157530f85724a6c9d4d to your computer and use it in GitHub Desktop.
Looks in PWD and above for a certain 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
#!/usr/bin/env bash | |
shopt -s extglob | |
function search_rc() { | |
local path_test="${PWD//${HOME}}" | |
if [[ ${#PWD} -eq ${#path_test} ]]; then | |
printf '%s\n' "Will not search below HOME" | |
return 1 | |
fi | |
! [[ $1 ]] && return 1 | |
IFS=/ | |
local file="${1}" | |
local p=($PWD) | |
local segment_count="${#p[@]}" | |
local z | |
for (( i=0; i<=segment_count-1; i++ )) | |
do | |
r=$(( segment_count - i )) | |
path_segment="${p[$r]}" | |
y="${path_segment}/${z}" | |
z="${y%%\/}" | |
dir_path="${PWD//$z}" | |
file_path="${dir_path//+(\/)//}/${file}" | |
if [[ -f "${file_path}" ]]; then | |
printf 'Found here: %s\n' "${file_path//+(\/)//}" | |
return 0 | |
fi | |
if [ "$dir_path" == "${HOME}" ]; then | |
printf '%s\n' "Not found." | |
return 1 | |
fi | |
done | |
} | |
if [[ ${BASH_SOURCE[0]} != $0 ]]; then | |
export -f search_rc | |
else | |
search_rc "${@}" | |
exit $? | |
fi | |
# humans | |
# mknod of #bash on freenode | |
# alternative | |
# IFS=/ read -r -a p <<< "$PWD"; declare -p p | |
# declare -a p=([0]="" [1]="Users" [2]="ramos" [3]="Google Drive" [4]="NobodyIsAboveTheLaw Coordinating Committee") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment