From 0ac5906eeee12a9f4a9ffdb2fb2d51f0cc5923d2 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 13 Nov 2020 10:25:04 -0800 Subject: [PATCH] refactor the way the module library is loaded added startup script callable from profile.d renamed setup.sh to load.sh added rsync copy function cprs added shell.env that is called by /etc/profile to set up access to this and related repos --- alias/00-basic | 8 ++-- alias/shell | 9 ++++ app/qt | 1 + env/00-shell.env | 30 -------------- env/01-opt.path | 4 -- env/01-path | 17 ++++++++ env/02-user.path | 9 ---- env/editor.env | 5 ++- options => env/options.env | 2 - function/{00-basic => 01-basic} | 4 +- function/01-path | 24 ----------- function/02-path | 49 ++++++++++++++++++++++ function/helpers | 45 -------------------- function/rsync | 13 ++++++ lang/node | 5 ++- load.sh | 40 ++++++++++++------ function/modules => module.lib | 8 ++++ modules/utility/filesystem.mod | 6 +++ modules/utility/helpers.mod | 13 ++++++ setup/.bash_profile | 6 --- setup/03-bash.sh | 20 --------- setup/bash.bashrc | 27 ------------ setup/etc/bash.bashrc | 10 +++++ setup/etc/profile | 62 ++++++++++++++++++++++++++++ setup/{ => etc/profile.d}/02-root.sh | 4 +- setup/etc/profile.d/03-startup.sh | 8 ++++ setup/profile | 37 ----------------- setup/root/.bash_profile | 6 +++ setup/root/.bashrc | 1 + setup/root/.profile | 9 ++++ setup/user/.bash_logout | 7 ++++ setup/user/.bash_profile | 5 +++ setup/{ => user}/.bashrc | 2 +- setup/user/.profile | 2 + shell.env | 60 +++++++++++++++++++++++++++ source-dir.func | 6 +-- function/startup => startup.sh | 18 ++++---- test/path.tst | 12 ++++++ 38 files changed, 357 insertions(+), 237 deletions(-) create mode 100644 alias/shell create mode 100644 app/qt delete mode 100644 env/00-shell.env delete mode 100644 env/01-opt.path create mode 100644 env/01-path delete mode 100644 env/02-user.path rename options => env/options.env (87%) rename function/{00-basic => 01-basic} (91%) delete mode 100644 function/01-path create mode 100644 function/02-path delete mode 100644 function/helpers create mode 100755 function/rsync rename function/modules => module.lib (94%) create mode 100644 modules/utility/filesystem.mod create mode 100644 modules/utility/helpers.mod delete mode 100644 setup/.bash_profile delete mode 100644 setup/03-bash.sh delete mode 100644 setup/bash.bashrc create mode 100644 setup/etc/bash.bashrc create mode 100644 setup/etc/profile rename setup/{ => etc/profile.d}/02-root.sh (50%) create mode 100755 setup/etc/profile.d/03-startup.sh delete mode 100644 setup/profile create mode 100644 setup/root/.bash_profile create mode 100644 setup/root/.bashrc create mode 100644 setup/root/.profile create mode 100644 setup/user/.bash_logout create mode 100644 setup/user/.bash_profile rename setup/{ => user}/.bashrc (89%) create mode 100644 setup/user/.profile create mode 100644 shell.env rename function/startup => startup.sh (70%) mode change 100644 => 100755 create mode 100755 test/path.tst diff --git a/alias/00-basic b/alias/00-basic index 73ba31b..026d650 100644 --- a/alias/00-basic +++ b/alias/00-basic @@ -1,7 +1,11 @@ # 00 will get loaded first +# does root login with calls profile and loads all the shells +alias sudo='sudo -i' + alias flist="function_list" alias flistg="function_list | grep" +alias fprint="declare -f" alias aliasg="alias | grep " alias envg="env | grep " @@ -11,9 +15,6 @@ alias envg="env | grep " alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' # brings user path to sudo -alias sudoi='sudo -i env PATH=$PATH' -alias sudoh='sudo -H' - alias reboot="sudo systemctl reboot" alias poweroff="sudo systemctl poweroff" @@ -49,6 +50,7 @@ alias cd..='cd ..' alias cp='cp -i' alias mpath="echo $PATH" # machine path per /etc/environment alias path='echo $PATH' +alias pathg='echo $PATH | grep' alias sysinfo='sudo inxi -Fzxd' alias efstab="cp /etc/fstab ~/fstab.bak && gedit admin:/etc/fstab" diff --git a/alias/shell b/alias/shell new file mode 100644 index 0000000..5e2654d --- /dev/null +++ b/alias/shell @@ -0,0 +1,9 @@ +alias ebashrc="gedit $HOME/.bashrc" +alias eetcbashrc="sudo gedit /etc/bash.bashrc" +alias eetcprofile="sudo gedit /etc/profile" +alias eprofile="gedit $HOME/.bash_profile" + +alias cbashrc="cp $HOME/.bashrc $BASH_SHELL_BASE/setup" +alias cetcbashrc="cp /etc/bash.bashrc $BASH_SHELL_BASE/setup" +alias cprofile="cp $HOME/.bash_profile $BASH_SHELL_BASE/setup" +alias cetcprofile="cp /etc/profile $BASH_SHELL_BASE/setup" diff --git a/app/qt b/app/qt new file mode 100644 index 0000000..22e0e52 --- /dev/null +++ b/app/qt @@ -0,0 +1 @@ +export QT_SELECT=qt5 diff --git a/env/00-shell.env b/env/00-shell.env deleted file mode 100644 index 773c84f..0000000 --- a/env/00-shell.env +++ /dev/null @@ -1,30 +0,0 @@ -# Additional Shell Repos to Source -# BASH_SHELL is a common parent directory that should be set in /etc/bash.bashrc -# unless below specific directories are set -# shell files are sourced in this order -# sourced via /etc/bash.bashrc so for all machine users -# $BASH_SHELL_BASE # this MUST be set in /etc/bash.bashrc - -# $BASH_SHELL_NETWORK/all -# $BASH_SHELL_NETWORK/$NETWORKNAME -# $BASH_SHELL_HOST/all -# $BASH_SHELL_HOST/ -# sourced via $HOME/.bashrc -# $HOME/shell or $HOME/BASH_SHELL_USER - -# within each of those directories if setup.sh exits it will be run -# otherwise files will be sourced exactly like in the base - -# identify a network name that this host resides on -# like hostname but for the LAN network -# if unset then only /all will be sourced -export NETWORKNAME=238 -# will use $BASH_SHELL_NETWORK/$NETWORKNAME under this directory unless specifically set -export BASH_SHELL_NETWORK=$BASH_SHELL/network - -# will use $BASH_SHELL_HOST/ unless specifically set -export BASH_SHELL_HOST=$BASH_SHELL/host - -# by default SHELL sources will be looked for under $HOME/bash/shell -# but can be user set below to $HOME/$BASH_SHELL_USER -# export BASH_SHELL_USER= diff --git a/env/01-opt.path b/env/01-opt.path deleted file mode 100644 index dd63931..0000000 --- a/env/01-opt.path +++ /dev/null @@ -1,4 +0,0 @@ -# include binaries in /optu -if [ -d "/opt/bin" ] ; then - PATH="/opt/bin:$PATH" -fi diff --git a/env/01-path b/env/01-path new file mode 100644 index 0000000..b060ab6 --- /dev/null +++ b/env/01-path @@ -0,0 +1,17 @@ +#!/bin/bash +# if [[ $(groups | grep sudo ) ]]; then +# path_insert /sbin after /usr/bin +# pathg bin +# path_insert /usr/sbin after :/sbin +# pathg bin +# path_insert /usr/local/sbin after /usr/sbin +# pathg sbin +# fi + + if [[ $(groups | grep sudo ) ]]; then + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + fi + +path_append /opt/bin +path_append "$HOME/bin" +path_append "$HOME/.local/bin" diff --git a/env/02-user.path b/env/02-user.path deleted file mode 100644 index 80c490d..0000000 --- a/env/02-user.path +++ /dev/null @@ -1,9 +0,0 @@ -# set PATH so it includes user's private bin if it exists -if [ -d "$HOME/bin" ] ; then - PATH="$HOME/bin:$PATH" -fi - -# set PATH so it includes user's private bin if it exists -if [ -d "$HOME/.local/bin" ] ; then - PATH="$HOME/.local/bin:$PATH" -fi diff --git a/env/editor.env b/env/editor.env index 487434e..bbef6e5 100644 --- a/env/editor.env +++ b/env/editor.env @@ -1,3 +1,4 @@ # used by aliases that need an editor -export EDITOR=atom -# export EDITOR=nano +# export EDITOR=atom +# export ELECTRON_TRASH=gio +export EDITOR=nano diff --git a/options b/env/options.env similarity index 87% rename from options rename to env/options.env index 685bf4d..0822e07 100644 --- a/options +++ b/env/options.env @@ -7,8 +7,6 @@ shopt -s checkwinsize # If set, the pattern "**" used in a pathname expansion context will # match all files and zero or more directories and subdirectories. #shopt -s globstar -# if expanding aliases in all non-interactive shell is desired then uncomment -shopt -s expand_aliases # enable programmable completion features (you don't need to enable # if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). diff --git a/function/00-basic b/function/01-basic similarity index 91% rename from function/00-basic rename to function/01-basic index e25951c..b81f2f7 100644 --- a/function/00-basic +++ b/function/01-basic @@ -5,6 +5,7 @@ function function_list() { if [ $1 ]; then + # will list functions in passed file local STR local func declare -a FUNCS @@ -13,13 +14,14 @@ function function_list() { for func in "${LIST[@]}" do if [[ ! $func =~ ^# ]]; then - func=${func#function } + func=${func#function } func=${func%()?( ){} [[ ! $func =~ ^_ ]] && FUNCS+=($func) fi done echo "${FUNCS[@]}" else + # will list all sourced/declared functions available echo echo -e "\033[1;4;32m""Functions:""\033[0;34m" declare -F | grep -v "declare -f\s_" | awk {'print $3'} diff --git a/function/01-path b/function/01-path deleted file mode 100644 index 0208ac1..0000000 --- a/function/01-path +++ /dev/null @@ -1,24 +0,0 @@ -path_remove () { - local IFS=':' - local NEWPATH - local DIR - local PATHVARIABLE=${2:-PATH} - for DIR in ${!PATHVARIABLE} ; do - if [ "$DIR" != "$1" ] ; then - NEWPATH=${NEWPATH:+$NEWPATH:}$DIR - fi - done - export $PATHVARIABLE="$NEWPATH" -} - -path_prepend () { - path_remove $1 $2 - local PATHVARIABLE=${2:-PATH} - export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}" -} - -path_append () { - path_remove $1 $2 - local PATHVARIABLE=${2:-PATH} - export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1" -} diff --git a/function/02-path b/function/02-path new file mode 100644 index 0000000..10ffc49 --- /dev/null +++ b/function/02-path @@ -0,0 +1,49 @@ +#!/bin/bash +path_remove () { + local IFS=':' + local NEWPATH + local DIR + local PATHVARIABLE=${2:-PATH} + for DIR in ${!PATHVARIABLE} ; do + if [ "$DIR" != "$1" ] ; then + NEWPATH=${NEWPATH:+$NEWPATH:}$DIR + fi + done + export $PATHVARIABLE="$NEWPATH" +} + +path_prepend () { + path_remove $1 $2 + if [ -d "$1" ]; then + local PATHVARIABLE=${2:-PATH} + export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}" + fi +} + +path_append () { + path_remove $1 $2 + if [ -d "$1" ]; then + local PATHVARIABLE=${2:-PATH} + export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1" + fi +} + +path_insert () { + if [ -d "$1" ]; then + local PATHVAR=${4:-PATH} + local temp + echo inserting $1 into ${!PATHVAR} $2 $3 + boa=$([[ $2 == b* ]] && echo "$1:&" || echo "&:$1:") + temp="$(echo ${!PATHVAR} | sed -r "s#$3(:)?#$boa#" | sed 's#^:##' | sed 's#\:$##' | sed 's#::#:#g' )" + echo result: $temp + export PATHVAR=$temp + fi +} + +# path_insert_before () { +# _path_insert b "$@" +# } +# +# path_insert_after () { +# _path_insert a "$@" +# } diff --git a/function/helpers b/function/helpers deleted file mode 100644 index ef4ac68..0000000 --- a/function/helpers +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/bash -valid_ip() -{ - local ip=$1 - local stat=1 - local res - - if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - OIFS=$IFS - IFS='.' - ip=($ip) - IFS=$OIFS - [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ - && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] - res=$([ $? == 0 ] && echo true || echo false) - else - res=false - fi - echo $res -} - -get_domain() { -local domain -domain=$(echo $1 | awk -F\. '{print $(NF-1) FS $NF}') -echo "$domain" -} - -# must be json as a string, depends on jq -get_prop_value () { - local value - # echo in $1 get $2 - value=$(echo $1 | jq -r .$2) - echo $value -} - -mounted () { -[[ ! $1 ]] && echo no mount point to test && return 2 - mountpoint "$1" &> /dev/null && echo yes || return 1 -} - - -is_array() { - local variable_name=$1 - [[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]] -} diff --git a/function/rsync b/function/rsync new file mode 100755 index 0000000..fc4495c --- /dev/null +++ b/function/rsync @@ -0,0 +1,13 @@ +#!/bin/bash +# simple copy using rsync. Preserves everything +# present working directory to supplied destination +module_load confirm +function cprs () { + [ $# -ne 2 ] && echo two directories source and destination need to be passed && return 1 + [ ! -d "$1" ] && echo source: $1 is not a directory && return 1 + [ ! -d "$2" ] && echo destination: $2 is not a directory && return 1 + echo Command to run: rsync --progress -a -r -u "$1" "$2" + confirm Do you want to start the rsync copy? || return 0 + echo copying..... + rsync --progress -a -r -u "$1" "$2" +} diff --git a/lang/node b/lang/node index 227917f..96016b2 100644 --- a/lang/node +++ b/lang/node @@ -1,9 +1,10 @@ #!/bin/bash if command -v node >/dev/null 2>&1; then +# echo addding node langauge stuff # gives access to packages with bin in a project -export PATH=$PATH:./node_modules/.bin -export PATH=$PATH:/opt/node-global-apps/bin +path_append ./node_modules/.bin +path_append /opt/node-global-apps/bin export NPM_REGISTRY=https://npm.kebler.net diff --git a/load.sh b/load.sh index d9c05ec..d47bd64 100755 --- a/load.sh +++ b/load.sh @@ -1,13 +1,27 @@ #!/bin/bash + +# don't bother if bash is not the shell +[ ! "$SHELL" = "/bin/bash" ] && return 1 + DIR=${1:-$(dirname ${BASH_SOURCE[0]})} # source the required directory source function if [ ! -e "$BASH_SHELL_BASE/source-dir.func" ]; then echo "!Unable to load sourcing function at $DIR/source-dir, shell not set up!" + return 1 else - . $BASH_SHELL_BASE/source-dir.func - # source the default shopt options - [ -e "$DIR/options" ] && . $DIR/options + source "$BASH_SHELL_BASE/source-dir.func" +fi + + if [ ! -e "$BASH_SHELL_BASE/module.lib" ]; then + echo "!Unable to load module library functions function at $DIR/module.lib, shell not set up!" + return 1 + else + source "$BASH_SHELL_BASE/module.lib" +fi + + # REMOVED sourced via env directory + # [ -e "$DIR/options" ] && . $DIR/options function shell_process_directory () { local SUBDIRS @@ -18,7 +32,7 @@ else # echo soucring directory $DIR if [ -d "$DIR" ]; then - if [ "$DIR" = "$BASH_SHELL_BASE" ] && [ "$BASH_SHELL_BASE_SOURCED" = "true" ]; then + if [ "$DIR" = "$BASH_SHELL_BASE" ] && [ "$BASH_SHELL_BASE_LOADED" = "true" ]; then if [ -v PS1 ]; then echo base directory already sourced read -p "do you want to re-source the base (not recommended)? " -n 1 -r @@ -31,7 +45,7 @@ else fi if [ -f "$DIR/$BASH_SHELL_LOAD" ] && [ "$DIR" != "$BASH_SHELL_BASE" ]; then - echo "$DIR/$BASH_SHELL_LOAD" found, running instead of default processing + # echo "$DIR/$BASH_SHELL_LOAD" found, running instead of default processing source "$DIR/$BASH_SHELL_LOAD" else # default processing @@ -39,7 +53,7 @@ else local DSUBDIRS # default subdirectories to source # TODO allow passing in additional directories - DSUBDIRS="function env alias misc lang app" + DSUBDIRS="function alias env misc lang app" IFS=' ' read -r -a SUBDIRS <<< "${2:-$DSUBDIRS}" # echo ${SUBDIRS[@]} for SUBDIR in "${SUBDIRS[@]}" @@ -62,18 +76,20 @@ else } # process the base directory by default -unset BASH_SHELL_BASE_SOURCED -shell_process_directory -export BASH_SHELL_BASE_SOURCED=true +unset BASH_SHELL_BASE_LOADED +unset BASH_SHELL_LOADED + +shell_process_directory # process base +export BASH_SHELL_BASE_LOADED=true # process network shell_process_directory "$BASH_SHELL_NETWORK/all" [ $NETWORKNAME ] && shell_process_directory "$BASH_SHELL_NETWORK/$NETWORKNAME" # process host shell_process_directory "$BASH_SHELL_HOST/all" shell_process_directory "$BASH_SHELL_HOST/$(hostname)" + +export BASH_SHELL_LOADED=true + # process user # Note: $HOME/shell or $HOME/BASH_SHELL_USER are sourced via $HOME/.bashrc - # echo $(envg BASH) | xargs -n 1 - -fi diff --git a/function/modules b/module.lib similarity index 94% rename from function/modules rename to module.lib index 16ba7e8..bd69036 100644 --- a/function/modules +++ b/module.lib @@ -98,6 +98,11 @@ module_loaded () { return $ret } +# exported for login environment +export -f module_load +export -f module_find +export -f module_confirm + function test_modules { RED='\033[0;31m' NC='\033[0m' # No Color @@ -117,3 +122,6 @@ echo ---------------------- echo -e "testing: ${RED}module_loaded${NC}" module_loaded $1 } + +#comment this out if testing +unset -f test_modules diff --git a/modules/utility/filesystem.mod b/modules/utility/filesystem.mod new file mode 100644 index 0000000..6b8bb3f --- /dev/null +++ b/modules/utility/filesystem.mod @@ -0,0 +1,6 @@ +#!/bin/bash +# must be json as a string, depends on jq +mounted () { +[[ ! $1 ]] && echo no mount point to test && return 2 + mountpoint "$1" &> /dev/null && echo yes || return 1 +} diff --git a/modules/utility/helpers.mod b/modules/utility/helpers.mod new file mode 100644 index 0000000..0d71830 --- /dev/null +++ b/modules/utility/helpers.mod @@ -0,0 +1,13 @@ +#!/bin/bash +# must be json as a string, depends on jq +get_prop_value () { + local value + # echo in $1 get $2 + value=$(echo $1 | jq -r .$2) + echo $value +} + +is_array() { + local variable_name=$1 + [[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]] +} diff --git a/setup/.bash_profile b/setup/.bash_profile deleted file mode 100644 index db35425..0000000 --- a/setup/.bash_profile +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -# uncomment for debuggin -echo ".bash_profile sourced at user $USER login" - -[[ -f "$HOME/.bashrc" ]] && source "$HOME/.bashrc" -[[ $- == *i* ]] || statup_load $HOME/$BASH_SHELL_USER diff --git a/setup/03-bash.sh b/setup/03-bash.sh deleted file mode 100644 index 2f8bec6..0000000 --- a/setup/03-bash.sh +++ /dev/null @@ -1,20 +0,0 @@ -# source bash.bashrc for both login and non login shells -# note: bash.bashrc sources the base shell repo -echo in profile.d/bash.sh - if [ "${SHELL-}" ] && [ "$SHELL" != "/bin/sh" ] && [ -f /etc/bash.bashrc ]; then - # uncomment for debugging - echo sourcing bash.bashrc now from /etc/profile.d/bash.sh - source /etc/bash.bashrc - [ $EUID -ne 0 ] && startup_load # if not root source this - else # set up basic stuff (e.g a prompt) if not able to source bash.bashrc - echo Unable to source bash.bashrc, Setting up red prompt for root and a green one for users. - NORMAL="\[\e[0m\]" - RED="\[\e[1;31m\]" - GREEN="\[\e[1;32m\]" - if [[ $EUID == 0 ]] ; then - PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL" - else - PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL" - fi - unset RED GREEN NORMAL -fi diff --git a/setup/bash.bashrc b/setup/bash.bashrc deleted file mode 100644 index 0422303..0000000 --- a/setup/bash.bashrc +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -# uncomment these for debugging. -echo sourcing system wide bash.bashrc -[ $EUID -eq 0 ] && echo 'Root User' || echo 'Non Root User' -[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive' -shopt -q login_shell && echo 'Login shell' || echo 'Not login shell' - -# Using base shell setup repository -# https://git.kebler.net/kebler.net/bash-shell-base.git -export BASH_SHELL_LOAD=load.sh # script to run that loads in base shell repo -# use these two if you want a common directory for all shell repos -export BASH_SHELL=/opt/bash/shell # set this based on where you cloned the base repo -export BASH_SHELL_BASE=$BASH_SHELL/base -# otherwise uncomment -# export BASH_SHELL_BASE=/opt/shell/base # set this based on where you cloned the base repo -# if uncommented next line sets up implicit sourcing for non-interactive shells -export BASH_ENV=$BASH_SHELL_BASE/$BASH_SHELL_LOAD # use base repo -# this gets the ball rolling my loading the base shell -source $BASH_SHELL_BASE/$BASH_SHELL_LOAD - -# if not using implicit sourcing for non-interactive shells then on can do this per script likely -################## -# source $BASH_SHELL_BASE/load.sh -# shopt -s expand_aliases -# < your script code > -# shopt -u expand_aliases -#################### diff --git a/setup/etc/bash.bashrc b/setup/etc/bash.bashrc new file mode 100644 index 0000000..78c9354 --- /dev/null +++ b/setup/etc/bash.bashrc @@ -0,0 +1,10 @@ +#!/bin/bash +# uncomment these for debugging. +# echo ---- sourcing system wide bash.bashrc --- +# [ $EUID -eq 0 ] && echo 'Root User' || echo 'Non Root User' +# [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive' +# shopt -q login_shell && echo 'Login shell' || echo 'Not login shell' +# echo ---- +# echo sourcing $BASH_SHELL_LOAD + +source $BASH_SHELL_LOAD diff --git a/setup/etc/profile b/setup/etc/profile new file mode 100644 index 0000000..bd16798 --- /dev/null +++ b/setup/etc/profile @@ -0,0 +1,62 @@ +#!/bin/bash + +# put no script code directly here. +# organize all script code in files /etc/profile.d + +# uncomment for debugging +# echo ---- system profile /etc/profile sourced at login ---- +# [ $EUID -eq 0 ] && echo 'Root User' || echo 'Non Root User' +# [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive' +# shopt -q login_shell && echo 'Login shell' || echo 'Not login shell' +# echo ----------- + +# main /etc/profile loaded for all logins +# more info see http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html + +export LOGIN_LOG=$HOME/logs/login.log +mkdir -p $HOME/logs +touch $LOGIN_LOG +llog () { + echo "$@" 2>&1 | tee -a $LOGIN_LOG +} + export -f llog + +# Set the initial path +export PATH=/bin:/usr/bin:/usr/local/bin + +# set directory for base shell repo +export BASH_SHELL_BASE=/opt/bash/shell/base # where +# now bootstrap by souring the shell repo envinroment +. $BASH_SHELL_BASE/shell.env + +llog "$(env | grep BASH)" + +if [[ $- == *i* ]]; then # interactive? + echo interactive login shell + if [[ ! $NO_LOGIN_BASHRC ]]; then + echo loading $BASH_SHELL_LOAD + source "$BASH_SHELL_LOAD" + else + NORMAL="\[\e[0m\]" + RED="\[\e[1;31m\]" + GREEN="\[\e[1;32m\]" + if [[ $EUID == 0 ]] ; then + PS1="$RED\u [ $NORMAL\w$RED ]# $NORMAL" + else + PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL" + fi + unset RED GREEN NORMAL + fi +else + echo "$(date)" > $LOGIN_LOG + llog "non-interactive login shell for $USER" + if [ -d /etc/profile.d ]; then + for i in /etc/profile.d/*.sh; do + if [ -r $i ]; then + llog "sourcing $i" + source $i + fi + done + unset i + fi +fi diff --git a/setup/02-root.sh b/setup/etc/profile.d/02-root.sh similarity index 50% rename from setup/02-root.sh rename to setup/etc/profile.d/02-root.sh index 65ca4a4..0d85643 100644 --- a/setup/02-root.sh +++ b/setup/etc/profile.d/02-root.sh @@ -1,6 +1,6 @@ # root login setup only, put in if block if [ $EUID -eq 0 ] ; then # if root user - echo root specific setup - export PATH=$PATH:/sbin:/usr/sbin + echo login profile, root specific setup + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin unset HISTFILE fi diff --git a/setup/etc/profile.d/03-startup.sh b/setup/etc/profile.d/03-startup.sh new file mode 100755 index 0000000..fd2104e --- /dev/null +++ b/setup/etc/profile.d/03-startup.sh @@ -0,0 +1,8 @@ +# this runs startups for bash shell base system + if [ "$SHELL" = "/bin/bash" ] && [ "${BASH_SHELL_STARTUP}" ]; then + # uncomment for debugging + if [[ -f $BASH_SHELL_STARTUP ]] && [[ $EUID -ne 0 ]]; then + llog "running startup script $BASH_SHELL_STARTUP async" + (${BASH_SHELL_STARTUP}) & + fi + fi diff --git a/setup/profile b/setup/profile deleted file mode 100644 index 796051b..0000000 --- a/setup/profile +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash -# main /etc/profile loaded for all logins -# more info see http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html - - -# Set the initial path -export PATH=/bin:/usr/bin -# uncomment for debugging -echo system profile /etc/profile sourced at login - -# put no script code directly here. -# organize all script code in files /etc/profile.d -# sourcing -if [[ $- == *i* ]] && [[ $EUID -ne 0 ]]; then - echo non root interactive login shell, will not source /etc/profile.d - if [[ $FORCE_LOAD_BASH_SHELL_BASE ]]; then - echo "forcing source of shell base: $BASH_SHELL_BASE/$BASH_SHELL_LOAD" - source "$BASH_SHELL_BASE/$BASH_SHELL_LOAD" - else - BASH_SHELL_BASE_SOURCED=false - NORMAL="\[\e[0m\]" - RED="\[\e[1;31m\]" - GREEN="\[\e[1;32m\]" - PS1="$GREEN\u [ $NORMAL\w$GREEN ]\$ $NORMAL" - unset RED GREEN NORMAL - fi -else - echo sourcing /etc/profile.d - if [ -d /etc/profile.d ]; then - for i in /etc/profile.d/*.sh; do - if [ -r $i ]; then - source $i - fi - done - unset i - fi -fi diff --git a/setup/root/.bash_profile b/setup/root/.bash_profile new file mode 100644 index 0000000..c3ab5e2 --- /dev/null +++ b/setup/root/.bash_profile @@ -0,0 +1,6 @@ +# echo $USER .bash_profile + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + source $BASH_SHELL_LOAD + if [ -f ~/.bashrc ]; then + . ~/.bashrc + fi diff --git a/setup/root/.bashrc b/setup/root/.bashrc new file mode 100644 index 0000000..a988723 --- /dev/null +++ b/setup/root/.bashrc @@ -0,0 +1 @@ +# echo $USER .bashrc diff --git a/setup/root/.profile b/setup/root/.profile new file mode 100644 index 0000000..a629083 --- /dev/null +++ b/setup/root/.profile @@ -0,0 +1,9 @@ +# ~/.profile: executed by Bourne-compatible login shells. + +echo $USER .profile + +if [ "$BASH" ]; then + if [ -f ~/.bashrc ]; then + . ~/.bashrc + fi +fi diff --git a/setup/user/.bash_logout b/setup/user/.bash_logout new file mode 100644 index 0000000..de4f5f7 --- /dev/null +++ b/setup/user/.bash_logout @@ -0,0 +1,7 @@ +# ~/.bash_logout: executed by bash(1) when login shell exits. + +# when leaving the console clear the screen to increase privacy + +if [ "$SHLVL" = 1 ]; then + [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q +fi diff --git a/setup/user/.bash_profile b/setup/user/.bash_profile new file mode 100644 index 0000000..6bb4ec7 --- /dev/null +++ b/setup/user/.bash_profile @@ -0,0 +1,5 @@ +#!/bin/bash +# uncomment for debuggin +echo ".bash_profile sourced at user $USER login" + +# [[ $- == *i* ]] && [[ -f "$HOME/.bashrc" ]] && source "$HOME/.bashrc" diff --git a/setup/.bashrc b/setup/user/.bashrc similarity index 89% rename from setup/.bashrc rename to setup/user/.bashrc index d062c82..2bf20f8 100644 --- a/setup/.bashrc +++ b/setup/user/.bashrc @@ -1,5 +1,5 @@ #!/bin/bash -echo "$USER .bashrc" +# echo "$USER .bashrc" # processing user's shell directory [[ $BASH_SHELL_BASE_SOURCED = true ]] && \ diff --git a/setup/user/.profile b/setup/user/.profile new file mode 100644 index 0000000..21f7625 --- /dev/null +++ b/setup/user/.profile @@ -0,0 +1,2 @@ + echo .profile, user $USER + echo "this only is sourced if .bash_profile doesn't exist" diff --git a/shell.env b/shell.env new file mode 100644 index 0000000..c6bf873 --- /dev/null +++ b/shell.env @@ -0,0 +1,60 @@ +# Shell Repos to Source + +# if bash is not the shell don't bother to continue +[ ! "$SHELL" = "/bin/bash" ] && return 1 + +# sourced for non-login interactive shells +# sourced via /etc/bash.bashrc so for all machine users +# $BASH_SHELL_BASE # this MUST be set in /etc/profile +# $BASH_SHELL_NETWORK/all +# $BASH_SHELL_NETWORK/$NETWORKNAME +# $BASH_SHELL_HOST/all +# $BASH_SHELL_HOST/ +# also +# sourced via $HOME/.bashrc +# $HOME/shell or $HOME/BASH_SHELL_USER + +# for the rare interactive login shell +# if you don't want the repos above sourced uncomment this next line +# $NO_LOGIN_BASHRC=true +# this is not effect non-interactive login shells like at user first login + +# within each of those directories if load.sh exits it will be run +# otherwise files will be sourced exactly like load.sh in the base + +# Using base shell setup repository +# https://git.kebler.net/kebler.net/bash-shell-base.git + +# use these two if you want a common directory for all shell repos +# BASH_SHELL_BASE is set in /etc/profile + +# load script in base repo to run +export BASH_SHELL_LOAD=$BASH_SHELL_BASE/load.sh # load.sh is default +export BASH_SHELL_STARTUP=$BASH_SHELL_BASE/startup.sh # strtup.sh is default + +# if uncommented next line sets up implicit sourcing for non-interactive shells +export BASH_ENV=$BASH_SHELL_LOAD # use base repo +# if not using implicit sourcing for non-interactive shells then on can do this per script +################## +# source $BASH_SHELL_BASE/$BASH_SHELL_BASE/${$BASH_SHELL_LOAD:-load.sh} +# shopt -s expand_aliases +# < your script code > +# shopt -u expand_aliases +#################### + +# identify a network name that this host resides on +# like hostname but for the LAN network +# if unset then only /all will be sourced +export NETWORKNAME=238 +# will use $BASH_SHELL_NETWORK/$NETWORKNAME under this directory unless specifically set +export BASH_SHELL_NETWORK=/opt/bash/shell/network + +# will use $BASH_SHELL_HOST/ unless specifically set +export BASH_SHELL_HOST=/opt/bash/shell/host + +# by default SHELL sources will be looked for under $HOME/bash/shell +# but can be user set below to $HOME/$BASH_SHELL_USER +# export BASH_SHELL_USER= + +# load and export module loading functions library +source $BASH_SHELL_BASE/module.lib diff --git a/source-dir.func b/source-dir.func index 06f73b3..d74fdc4 100644 --- a/source-dir.func +++ b/source-dir.func @@ -68,7 +68,7 @@ local FIND FIND="find $DIR" FIND+=$([ ! $DEPTH == 0 ] && echo " -maxdepth $DEPTH ") # ignore all .name and .path by default -# TODO change to having a string +# TODO change to having a string FIND+=" -type f ! -path \"*/.*/*\" ! -name \".*\" " @@ -104,8 +104,8 @@ local FILES FILES=$(eval $FIND | sort) # echo $FILES | xargs -n 1 for f in $FILES; do - # echo sourcing: $f - . "$f" + # echo sourcing: $f + source "$f" done } diff --git a/function/startup b/startup.sh old mode 100644 new mode 100755 similarity index 70% rename from function/startup rename to startup.sh index 4b69328..e0b1ad7 --- a/function/startup +++ b/startup.sh @@ -5,15 +5,15 @@ local SDIRS local SDIR local DIRS DIRS=( \ -$BASH_SHELL_HOST/$(hostname) -$BASH_SHELL_HOST/all \ -$BASH_SHELL_NETWORK/$NETWORKNAME \ -$BASH_SHELL_NETWORK/all \ $BASH_SHELL_BASE \ +$BASH_SHELL_NETWORK/all \ +$BASH_SHELL_NETWORK/$NETWORKNAME \ +$BASH_SHELL_HOST/all \ +$BASH_SHELL_HOST/$(hostname) \ ) - +echo directories ${DIRS[*]} [[ $1 ]] && DIRS=("$1") -echo startup ${DIRS[@]} +llog "startup directories to try ${DIRS[*]}" SDIRS=() j=0 cnt=${#DIRS[@]} @@ -26,10 +26,14 @@ done [ $STARTUP_DIRS ] && SDIRS=("$STARTUP_DIRS" "${SDIRS[@]}") # echo -e "Statup Directories to source \n ${SDIRS[@]} \n" +source $BASH_SHELL_BASE/source-dir.func for SDIR in "${SDIRS[@]}" do - # echo startup dir to source $SDIR + llog "startup dir to source $SDIR" source_dir -p "archive" -x '"*.off" "*.md"' -d 0 $SDIR done } + +# if script was executed then call the function +(return 0 2>/dev/null) || startup_load diff --git a/test/path.tst b/test/path.tst new file mode 100755 index 0000000..de6b0ea --- /dev/null +++ b/test/path.tst @@ -0,0 +1,12 @@ +#!/bin/bash +echo path functions test script +src=$BASH_SHELL_BASE/function/01-path +[[ -f $src ]] && source "$src" || echo no file: $src +export test_path=/bin:/usr/bin:/opt/go/bin +ins=/usr/sbin +path_insert $ins after /usr/bin test_path +path_insert $ins before /usr/bin test_path +path_insert $ins after /opt/go/bin test_path +path_insert $ins before ^/bin: test_path +unset test_path +unset ins