Am 29.03.2013 12:57, schrieb Greg Wooledge: > On Fri, Mar 29, 2013 at 12:41:46AM -0700, Linda Walsh wrote: >> include was designed to search the path for functions that >> are relative paths. While the normal sourcepath allows searching for >> filenames on the search path, I don't believe (please correct if I am wrong >> and this works now, as it would make life much simpler) that the PATH will >> be searched if you give it something like: >> >> source lib/Util/sourcefile.shh > Is that all you want? Here: > > include() { > local paths dir > IFS=: read -ra paths <<< "$PATH" > for dir in "${paths[@]}"; do > if [[ -r $dir/$1 ]]; then > source "$dir/$1" > return > fi > done > echo "could not find '$1' in PATH" >&2 > return 1 > } > Actually I've had trouble
IFS=: read -ra paths <<< "$PATH" and embedded new lines. I think this is better find_file() { local IFS=: for dir in $PATH; do [[ ! -r $dir/$1 ]] || continue FOUND_FILE="$dir/$1" return 0 done echo "could not find '$1' in PATH" >&2 return 1 } include() { local FOUND_FILE find_file "${1:?Missing File Name }" || return $? source "${FOUND_FILE}" } includeExt() { local FOUND_FILE local PATH=${1:?Missing Search Path} find_file "${2:?Missing File Name}" || return $? source "${FOUND_FILE}" } Ideally what I want to do is alias include=source\ "$(find_file "${1}")" but that doesn't work in bash and I still haven't found a way around the problem. The only way I can think to do it is to use a second file. cat <<EOF ><known_path>/source_wrapper.sh find_file "${1:?Missing File Name }" || return $? source "${FOUND_FILE}" EOF alias include=source\ "<known_path>/source_wrapper.sh" cheers