Script that can source all files in a directory , with filter
#!/usr/bin/env bash # ############################################################################ # # PATH: /etc/profile.d AUTHOR: Hoefkens.J@gmail.com # # FILE: sourcedir.sh # ############################################################################ # ############################################################################ function sourcedir { local SRC local SELECTED local MATCH local I local N local W local GP local GC local GS local CONF local HELP local WARNING local selectmtd local SOURCEDIR SOURCEDIR="${FUNCNAME[0]}" WARNING="This File Needs to be Sourced not Executed !" function _cat { local concat [[ -n $( which bat) ]] && concat="bat --plain --language=help "|| concat="cat" [[ -z $COLORTERM ]] && concat="cat" printf "%s\n" "$1" | env "$concat" } function _help { local HELP HELP="$SOURCEDIR [-h] [DIR] [MATCH] Arguments: DIR Directory to source files from. Files that return True when tested aganst [MATCH] will be sourced MATCH String to match Files against. Globbing and Expansion follow Bash Settings Options: -h --help Show this help text -e --regex Use RegEx (case insensitive)to match files against -q --quiet Quiet/Silent/Script, Dont produce any output --warning Shows $WARNING Recommended: Make Sourcedir availeble as a command su -c 'cp -v ./sourcedir.sh /etc/profile.d/ Examples : MATCH : (does not recurse into directories) [1-9][0-9]*.{sh,bash,bashrc,cfg,conf,rc} : DEFAULT [0-9][0-9][0-9][_-]* : Matches: 000_anything 999- [0-9]??[_-]* : Matches: 109_myconfig.conf , 02a-myconfig [0-2]* : Matches: 2anythinghere *.sh : Matches: any.sh MATCH with -e|--regex flag enabled (recurses into directories): '/[0-9]+[_-]*.*\.(sh|bash|bashrc|rc|conf|cfg)$' : DEFAULT : Note: enclose the regex in '' or \"\" USE : source sourcedir.sh # (when not placed in /etc/profile.d) Source this file first sourcedir -q ~/.config/bashrc/ *.bashrc # source files in ~/.config/bashrc/ that end in '.bashrc' # and (-q) don't produce any output - as some apparently # interactive shells (scp,rcp,...) can't tolerate any output. sourcedir ~/.winepfx/protonGE/ [0-9][0-9]_* # source files starting with 2 digits + '_ ' in ~/.winepfx/protonGE/ " _cat "$HELP" } function _m { printf "\x1b[%s;3%sm%s\x1b[m" "${1}" "${2}" "${3}" ;} function _G { printf "\x1b[%sG" "${1}";} function _selectls { [[ -n "${2}" ]] && MATCH="${2}" || MATCH="[0-9][0-9]*.{sh,bash,bashrc,cfg,conf,rc}" SELECTED=$SRC/$MATCH N=$( env ls "${SELECTED}" 2>/dev/null|wc -l ) } function _selectre { [[ -n "${2}" ]] && MATCH="${2}" || MATCH='/[0-9]+[_-]*.*\.(sh|bash|bashrc|rc|conf|cfg)$' SELECTED=$( find "$SRC" 2>/dev/null |grep -iE "$MATCH" ) N=$( echo "$SELECTED" |wc -l ) } function _progress { _G 12 ; _m 1 3 "${SRC}" ; _G "${GC}" ; _m 1 2 "${1}" ; _m 1 7 "/" ; _m 1 2 "${N}"; _m 1 7 "]" ; } function _mask { _G 0 ; _m 0 7 "Sourcing:" ; _G "${GP}" ; _m 1 7 "["; _G "${GS}" ; _m 1 7 "/" ; _m 1 2 "${N}" ; _m 1 7 "]" ; } function _state { SRC=$(realpath "${1}") I=0 selectmtd "$@" W="${#N}" GP=$((75-6-W*2)) GC=$((GP+1)) GS=$((GP+W+1)) } function _sourcefiles { for CONF in $SELECTED ; do I=$((I+1)) [[ -r "$CONF" ]] && source "$CONF" && _progress "$I" done } function _main { [[ -z "$selectmtd" ]] && selectmtd=_selectls _state "$@" _mask "$@" _sourcefiles "$@" printf " \x1b[75G\x1b[32mDONE\n" } # select procedure case "$1" in ''|-h|--help) _help ;; -q|--quiet) shift 1 && ${FUNCNAME[0]} "$@" &> /dev/null ;; -e|--regex) selectmtd=_selectre && shift 1 && ${FUNCNAME[0]} "$@" ;; --warning) printf "\x1b[1;31m%s\x1b[m\n" "${WARNING}" >> /dev/stderr ;; *) _main "$@" ;; esac #Cleanup env : unset _m _G _progress _mask _state _sourcefiles _main _cat } (return 0 2>/dev/null) || sourcedir --warning
0 Comments
There are no comments to display.