Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # shellcheck shell=ksh
- # If SH_LIBPATH is not set or null, then we try to build it
- if [ -z "${SH_LIBPATH+x}" ] || [ "${#SH_LIBPATH}" -eq "0" ]; then
- for _path in /usr/local/lib/sh "${HOME}"/.local/lib/sh; do
- [ -d "${_path}" ] && SH_LIBPATH="${SH_LIBPATH}:${_path}"
- done
- fi
- unset -v _path
- # Remove any leading colons from the construction process and export
- SH_LIBPATH="${SH_LIBPATH#:}"
- export SH_LIBPATH
- # We want SH_LIBPATH to be expanded for printf
- # shellcheck disable=SC2086
- import() {
- while read -r _path; do
- if [ -r "${_path}"/"${1:?No target specified}" ]; then
- # shellcheck disable=SC1090
- . "${_path}"/"${1}"
- unset -v _path
- return 0
- fi
- done < <(IFS=':'; printf -- '%s ' ${SH_LIBPATH})
- unset -v _path
- return 1
- }
- # Function to work through a list of commands and/or files
- # and fail on any unmet requirements. Example usage:
- # requires curl sed awk /etc/someconf.cfg
- requires() {
- # shellcheck disable=SC2048
- for _fsobj in ${*}; do
- # First try to determine if it's a command
- command -v "${_fsobj}" >/dev/null 2>&1 && continue
- # Next, see if it's an executable file e.g. a script to call
- [ -x ./"${_fsobj}" ] && continue
- # Next, let's see if it's a readable file e.g. a cfg file to load
- [ -r "${_fsobj}" ] && continue
- # If we get to this point, add it to our list of failures
- _failures="${_failures} ${_fsobj}"
- done
- # Strip any leading space from the construction of _failures
- _failures="${_failures# }"
- # If we have no failures, then no news is good news - return quietly
- if [ "${#_failures}" -eq "0" ]; then
- unset -v _fsobj _failures
- return 0
- # Otherwise, we error out and exit immediately
- else
- printf -- '%s\n' "The following requirements were not met" "${_failures}" >&2
- unset -v _fsobj _failures
- exit 1
- fi
- }
- # Sometimes you might want to load a file only if it exists,
- # but otherwise it's not critical and your script can move on.
- load_if_found() {
- _fstarget="${1:?No target specified}"
- if [ -e "${_fstarget}" ]; then
- if [ -r "${_fstarget}" ]; then
- # shellcheck disable=SC1090
- . "${_fstarget}"
- unset -v _fstarget
- else
- printf -- '%s\n' "${_fstarget} exists but isn't readable" >&2
- unset -v _fstarget
- return 1
- fi
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement