Advertisement
Guest User

Untitled

a guest
Jan 11th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.33 KB | None | 0 0
  1. # shellcheck shell=ksh
  2. # If SH_LIBPATH is not set or null, then we try to build it
  3. if [ -z "${SH_LIBPATH+x}" ] || [ "${#SH_LIBPATH}" -eq "0" ]; then
  4.   for _path in /usr/local/lib/sh "${HOME}"/.local/lib/sh; do
  5.     [ -d "${_path}" ] && SH_LIBPATH="${SH_LIBPATH}:${_path}"
  6.   done
  7. fi
  8. unset -v _path
  9. # Remove any leading colons from the construction process and export
  10. SH_LIBPATH="${SH_LIBPATH#:}"
  11. export SH_LIBPATH
  12.  
  13. # We want SH_LIBPATH to be expanded for printf
  14. # shellcheck disable=SC2086
  15. import() {
  16.   while read -r _path; do
  17.     if [ -r "${_path}"/"${1:?No target specified}" ]; then
  18.       # shellcheck disable=SC1090
  19.       . "${_path}"/"${1}"
  20.       unset -v _path
  21.       return 0
  22.     fi
  23.   done < <(IFS=':'; printf -- '%s ' ${SH_LIBPATH})
  24.   unset -v _path
  25.   return 1
  26. }
  27.  
  28. # Function to work through a list of commands and/or files
  29. # and fail on any unmet requirements.  Example usage:
  30. # requires curl sed awk /etc/someconf.cfg
  31. requires() {
  32.   # shellcheck disable=SC2048
  33.   for _fsobj in ${*}; do
  34.     # First try to determine if it's a command
  35.     command -v "${_fsobj}" >/dev/null 2>&1 && continue
  36.  
  37.     # Next, see if it's an executable file e.g. a script to call
  38.     [ -x ./"${_fsobj}" ] && continue
  39.  
  40.     # Next, let's see if it's a readable file e.g. a cfg file to load
  41.     [ -r "${_fsobj}" ] && continue
  42.  
  43.     # If we get to this point, add it to our list of failures
  44.     _failures="${_failures} ${_fsobj}"
  45.   done
  46.   # Strip any leading space from the construction of _failures
  47.   _failures="${_failures# }"
  48.  
  49.   # If we have no failures, then no news is good news - return quietly
  50.   if [ "${#_failures}" -eq "0" ]; then
  51.     unset -v _fsobj _failures
  52.     return 0
  53.   # Otherwise, we error out and exit immediately
  54.   else
  55.     printf -- '%s\n' "The following requirements were not met" "${_failures}" >&2
  56.     unset -v _fsobj _failures
  57.     exit 1
  58.   fi
  59. }
  60.  
  61. # Sometimes you might want to load a file only if it exists,
  62. # but otherwise it's not critical and your script can move on.
  63. load_if_found() {
  64.   _fstarget="${1:?No target specified}"
  65.   if [ -e "${_fstarget}" ]; then
  66.     if [ -r "${_fstarget}" ]; then
  67.       # shellcheck disable=SC1090
  68.       . "${_fstarget}"
  69.       unset -v _fstarget
  70.     else
  71.       printf -- '%s\n' "${_fstarget} exists but isn't readable" >&2
  72.       unset -v _fstarget
  73.       return 1
  74.     fi
  75.   fi
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement