refactor shell environmnet so user can have create customized shell.env

refactor default loading of networks
master
Kebler Network System Administrator 2022-12-30 09:18:39 -08:00
parent 34311b8183
commit bf62d67339
7 changed files with 125 additions and 82 deletions

View File

@ -45,7 +45,7 @@ export BASH_SHELL_NETWORK=$BASH_SHELL/network
export BASH_SHELL_HOST=$BASH_SHELL/host export BASH_SHELL_HOST=$BASH_SHELL/host
# by default SHELL sources will be looked for under $HOME/shell # by default SHELL sources will be looked for under $HOME/shell
# but can be user set below to $HOME/$BASH_SHELL_USER # but can be user set below to BASH_SHELL_USER_DIR
# export BASH_SHELL_USER=<some directory under $HOME> # export BASH_SHELL_USER=<some directory under $HOME>
if there is a script setup.sh in the root of these directoies it will be run. If not then all files no matter how deep will be sourced in alpha order order. if there is a script setup.sh in the root of these directoies it will be run. If not then all files no matter how deep will be sourced in alpha order order.

81
load.sh
View File

@ -1,30 +1,5 @@
#!/bin/bash #!/bin/bash
reload_shell () {
source $BASH_SHELL_BASE/load.sh $@
}
load_shell_host () {
module_load shell-process-directory
[[ $BASH_SHELL_HOST ]] && shell_process_directory $BASH_SHELL_HOST
}
load_shell_user () {
module_load shell-process-directory
[[ -d $HOME/${BASH_SHELL_USER:-shell} ]] && shell_process_directory $HOME/${BASH_SHELL_USER:-shell}
}
load_shell_dev () {
module_load shell-process-directory
[[ $BASH_SHELL_DEV ]] && shell_process_directory $BASH_SHELL_DEV
}
load_shell_host_user_dev () {
load_shell_host
load_shell_user
load_shell_dev
}
# PRIMARY SHELL LOAD SCRIPT - RUN FOR ALL INTERACTIVE SHELLS # PRIMARY SHELL LOAD SCRIPT - RUN FOR ALL INTERACTIVE SHELLS
# don't bother if bash is not the shell # don't bother if bash is not the shell
@ -42,6 +17,42 @@ function debug () {
} }
fi fi
reload_shell () {
source $BASH_SHELL_BASE/load.sh $@
}
load_custom_shell_env () {
[[ -f $BASH_SHELL_HOST/shell.env ]] && source $BASH_SHELL_HOST/shell.env
[[ -f $HOME/${BASH_SHELL_USER:-shell}/shell.env ]] && source $HOME/${BASH_SHELL_USER:-shell}/shell.env
[[ -f $BASH_SHELL_DEV/shell.env ]] && shell_process_directory $BASH_SHELL_DEV/shell.env
}
load_shell_host () {
module_load shell-process-directory
[[ -d $BASH_SHELL_HOST ]] && shell_process_directory $BASH_SHELL_HOST
}
load_shell_user () {
module_load shell-process-directory
BASH_SHELL_USER_DIR=${BASH_SHELL_USER_DIR:-$HOME/shell}
[[ -d $BASH_SHELL_USER_DIR ]] && shell_process_directory $BASH_SHELL_USER_DIR
}
load_shell_dev () {
module_load shell-process-directory
[[ $BASH_SHELL_DEV ]] && shell_process_directory $BASH_SHELL_DEV
}
load_shell_host_user_dev () {
load_shell_host
load_shell_user
load_shell_dev
}
### START LOAD SCRIPT #####
load_custom_shell_env
([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases ([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases
# process the base directory by default # process the base directory by default
@ -60,10 +71,10 @@ module_load shell-process-directory
# shell_process_directory $BASH_SHELL_BASE # shell_process_directory $BASH_SHELL_BASE
# shell_process_directory $BASH_SHELL_ANY_HOST # shell_process_directory $BASH_SHELL_ANY_HOST
# shell_process_directory $BASH_SHELL_HOST # shell_process_directory $BASH_SHELL_HOST
# export BASH_SHELL_BASE_LOADED=true # export BASH_SHELL_BASE_LOADED=true
# process base any # process base and any host and any network directories
# as set in shell.env
for dir in ${1:-$BASH_SHELL_DIRS}; do for dir in ${1:-$BASH_SHELL_DIRS}; do
# env | grep BASH # env | grep BASH
# echo base $dir # echo base $dir
@ -71,15 +82,19 @@ for dir in ${1:-$BASH_SHELL_DIRS}; do
[[ "$dir" = "$BASH_SHELL_BASE" ]] && export BASH_SHELL_BASE_LOADED=true [[ "$dir" = "$BASH_SHELL_BASE" ]] && export BASH_SHELL_BASE_LOADED=true
done done
# set one or the other of these in shell.env if you want to load networks with every shell
if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] || [[ $BASH_SHELL_NETWORKS_LOAD ]] || [[ $BASH_SHELL_NETWORK ]] ; then # # set one or the other of these in shell.env if you want to load networks with every shell
if [[ $BASH_SHELL_ANY_NETWORK ]]; then if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] || [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]] || [[ $BASH_SHELL_NETWORKS_LOAD ]] ; then
module_load networks module_load networks
[[ $(is_function _load_networks) ]] && _load_networks $BASH_SHELL_NETWORK if [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]]; then
networks_load -a
else
[[ $BASH_SHELL_HOME_NETWORK_LOAD ]] && networks_load -h
fi fi
[[ $BASH_SHELL_NETWORKS_LOAD ]] && networks_load $BASH_SHELL_NETWORKS_LOAD
fi fi
load_shell_host_user_dev load_shell_host
export BASH_SHELL_LOADED=true export BASH_SHELL_LOADED=true

View File

@ -56,7 +56,7 @@ fi
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched # BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
# in revsere order, user or network repos over host over base # in revsere order, user or network repos over host over base
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ) DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} )
[ -d $HOME/$BASH_SHELL_USER ] && DIRS=("${DIRS[@]}" "$HOME/$BASH_SHELL_USER") [ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV") [ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
cnt=${#DIRS[@]} cnt=${#DIRS[@]}

View File

@ -1,5 +1,31 @@
#!/bin/bash #!/bin/bash
shell_source_subdirs () {
# default processing
[[ ! -d $1 ]] && echo no directory $1 from which to process subdirectories && return 1
local DIR=
local SUBDIRS
local IGNORE_FILE
DIR=$1
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# echo $DIR
# echo $SUBDIRS
for SUBDIR in $SUBDIRS; do
if [ -e "$DIR/$SUBDIR" ]; then
# echo processing subdirectory $DIR/$SUBDIR
# must quote all globs to avoid bash glob expansion which is likely on
# TODO have default set of ignores plus check for shell-ignore
# source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# else
# echo no subdirectory $DIR/$SUBDIR to process, ignorning
fi
done
}
function shell_process_directory () { function shell_process_directory () {
local SUBDIRS local SUBDIRS
local DIR local DIR
@ -7,8 +33,8 @@ function shell_process_directory () {
module_load file module_load file
[[ $? -ne 0 ]] && echo unable to access the file module, aborting load && return 1 [[ $? -ne 0 ]] && echo unable to access the file module, aborting load && return 1
DIR=${1:-$BASH_SHELL_BASE} DIR=${1:-$BASH_SHELL_BASE}
# echo sourcing directory $DIR # echo sourcing directory $DIR
# if [[ $DIR = "$BASH_SHELL_BASE" ]]; then # if [[ $DIR = "$BASH_SHELL_BASE" ]]; then
# BASH_SHELL_IGNORE=$(shell_get_ignores) # BASH_SHELL_IGNORE=$(shell_get_ignores)
@ -32,29 +58,31 @@ function shell_process_directory () {
fi fi
if [[ $DIR != "$BASH_SHELL_BASE" ]] && [[ -f $DIR/load.sh ]]; then if [[ $DIR != "$BASH_SHELL_BASE" ]] && [[ -f $DIR/load.sh ]]; then
# echo "$DIR/load.sh" found, running instead of default load #
echo "$DIR/load.sh" found, running instead of base load.sh script
source "$DIR/load.sh" source "$DIR/load.sh"
else else
# default processing shell_source_subdirs $DIR
local SUBDIRS # # default processing
local IGNORE_FILE # local SUBDIRS
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include") # local IGNORE_FILE
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session" # SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore" # [[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
# echo $DIR # IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# echo $SUBDIRS # # echo $DIR
for SUBDIR in $SUBDIRS; do # # echo $SUBDIRS
if [ -e "$DIR/$SUBDIR" ]; then # for SUBDIR in $SUBDIRS; do
# echo processing subdirectory $DIR/$SUBDIR # if [ -e "$DIR/$SUBDIR" ]; then
# must quote all globs to avoid bash glob expansion which is likely on # # echo processing subdirectory $DIR/$SUBDIR
# TODO have default set of ignores plus check for shell-ignore # # must quote all globs to avoid bash glob expansion which is likely on
# source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR # # TODO have default set of ignores plus check for shell-ignore
# source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR # # source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR # # source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
# else # source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# echo no subdirectory $DIR/$SUBDIR to process, ignorning # # else
fi # # echo no subdirectory $DIR/$SUBDIR to process, ignorning
done # fi
# done
fi fi
# echo "done sourcing directory $DIR" # echo "done sourcing directory $DIR"
# else # else

View File

@ -14,9 +14,9 @@ isFile() {
fi fi
} }
# env_file_pretty () { #TODO
# env_file_pretty () {
# } # }
# https://www.baeldung.com/linux/envsubst-command # https://www.baeldung.com/linux/envsubst-command
env_subs_file () { env_subs_file () {
@ -60,14 +60,16 @@ env_subs_file () {
build_file () { build_file () {
# local file # local file
# echo build file $1 $2 # echo build file $1 $2
# todo merge no cr
[[ $1 == "-f" ]] && file=true && shift 1 [[ $1 == "-f" ]] && file=true && shift 1
[[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1) [[ -f "$2" ]] || { iecho "output file $2 does not exist"; return 1; }
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
# echo $1 # echo adding file $1 to $2
[[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2 [[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2
# remove comment lines, remove blank last line # remove comment lines, remove blank last line
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2 cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
else iecho "no such file $1 to append" [[ ! $file ]] && echo -e "\n" >> $2
else iecho "no such file $1 to append to $2"
fi fi
} }

View File

@ -11,9 +11,9 @@
if ( [[ $SHELL_INTERACTIVE ]] );then if ( [[ $SHELL_INTERACTIVE ]] );then
if ( [[ $BASH_SHELL_BASE_LOADED = true ]] ) ; then if ( [[ $BASH_SHELL_BASE_LOADED = true ]] ) ; then
export BASH_SHELL_USER=${BASH_SHELL_USER:-"shell"} export BASH_SHELL_USER=${BASH_SHELL_USER:-"shell"}
# echo loading user $USER shell at $HOME/$BASH_SHELL_USER # echo loading user $USER shell at BASH_SHELL_USER_DIR
[[ -d $HOME/$BASH_SHELL_USER ]] && shell_process_directory "$HOME/$BASH_SHELL_USER" ||\ [[ -d BASH_SHELL_USER_DIR ]] && shell_process_directory "BASH_SHELL_USER_DIR" ||\
echo no user shell directory $HOME/$BASH_SHELL_USER to process, create one or clone a template echo no user shell directory BASH_SHELL_USER_DIR to process, create one or clone a template
fi fi
# uncomment to add non-interactive setup/sourcing # uncomment to add non-interactive setup/sourcing
# else # else

View File

@ -4,6 +4,10 @@
# In a nutshell its a set of directories that are sourced when creating a shell # In a nutshell its a set of directories that are sourced when creating a shell
# The repos allow one to organize functionality across machines, users and networks # The repos allow one to organize functionality across machines, users and networks
# In general do not change or edit this base shell.env file
# Instead add a custom shell.env to specific shell host directory,
# user shell directory or dev shell directory
# echo DEBUG: sourcing shell.env # echo DEBUG: sourcing shell.env
# This file gets sourced early in /etc/profile for all login and # This file gets sourced early in /etc/profile for all login and
@ -15,7 +19,6 @@
# with NO_BASH_SHELL_SSH=true # with NO_BASH_SHELL_SSH=true
# this does not effect non-interactive login shells like at user first login # this does not effect non-interactive login shells like at user first login
# if bash is not installed then don't bother to continue # if bash is not installed then don't bother to continue
! command -v bash >/dev/null 2>&1 && echo no bash command && return 1 ! command -v bash >/dev/null 2>&1 && echo no bash command && return 1
export SHELL=$(command -v bash ) export SHELL=$(command -v bash )
@ -98,41 +101,34 @@ BASH_SHELL_DEV=${BASH_SHELL_DEV:-/opt/shell}
[[ -d $BASH_SHELL_DEV ]] && export BASH_SHELL_DEV [[ -d $BASH_SHELL_DEV ]] && export BASH_SHELL_DEV
# by default USER shell sources will be looked for under $HOME/shell # by default USER shell sources will be looked for under $HOME/shell
# but the default directory for ALL users can be set manually below # export BASH_SHELL_USER_DIR=$HOME/shell
# export BASH_SHELL_USER=<some directory under $HOME>
export BASH_SHELL_USER=${BASH_SHELL_USER:-shell}
# or add this export to .bashrc for custom directory. This can be done during or after using the user setup script
# set up default location to look for other network shell directories # set up default location to look for other network shell directories
BASH_NETWORKS_DIR=$parent/networks BASH_NETWORKS_DIR=$parent/networks
# BASH_NETWORKS_DIR=<some directory> # alt directory to look for networks # BASH_NETWORKS_DIR=<some directory> # alt directory to look for networks
export BASH_NETWORKS_DIR export BASH_NETWORKS_DIR
# set one or the other of these if you want to load networks with every shell # set one or the other of these here for all users or put in shell.env under HOST/USER/DEV directories
export BASH_SHELL_HOME_NETWORK_LOAD=true # if you want to load networks with every shell
# export BASH_SHELL_HOME_NETWORK_LOAD=true
# export BASH_SHELL_NETWORKS_LOAD=true # export BASH_SHELL_NETWORKS_LOAD=true
# export BASH_SHELL_NETWORK=<some directory> # export BASH_SHELL_NETWORK=<some directory>
# export BASH_SHELL_NETWORK=238.kebler.net
# now load and export module loading functions library so it is always available # now load and export module loading functions library so it is always available
source $BASH_SHELL_BASE/module.lib source $BASH_SHELL_BASE/module.lib
# echo done shell.env
# echo ALL DIRS: $BASH_SHELL_DIRS # echo ALL DIRS: $BASH_SHELL_DIRS
# cat $HOME/.bashrc
# source "$HOME/.bashrc"
# env | grep BASH # env | grep BASH
# env | grep NETWORK_DIRS # env | grep NETWORK_DIRS
# echo --------------------------------- # echo ---------------------------------
# echo $BASH_SHELL_DIRS # echo $BASH_SHELL_DIRS
# echo --------------------------------- # echo ---------------------------------
# echo end shell env
#archived ---for deletion or better explanation #archived ---for deletion or better explanation
# echo enabling aliases with non-interactive shell # echo enabling aliases with non-interactive shell
# DEPRECATED # export BASH_USE_ALIAS=true # will source aliases for non-interactive # DEPRECATED # export BASH_USE_ALIAS=true # will source aliases for non-interactive
# if not using implicit sourcing for non-interactive shells then one can do this per script # if not using implicit sourcing for non-interactive shells then one can do this per script
@ -143,3 +139,5 @@ source $BASH_SHELL_BASE/module.lib
# < your script code > # < your script code >
# shopt -u expand_aliases # shopt -u expand_aliases
#################### ####################