From e25e44a98dd0ab2234ac64cde374022200e1fe87 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Tue, 10 Nov 2020 14:09:08 -0800 Subject: [PATCH] added setup directory with set of user and system source files that can be copied in new machine changed setup.sh to load.sh startup only loads system repos by default and allows passing directory argument --- .gitignore | 1 + README.md | 4 +++ app/pio | 4 --- env/01-opt.path | 2 +- env/ssh.env | 2 -- function/01-path | 24 +++++++++++++++++ function/helpers | 5 ++++ function/logit | 10 +++++++ function/modules | 5 +++- function/ssh | 4 --- function/startup | 35 +++++++++++++++++++++++++ setup.sh => load.sh | 12 +++++---- misc/00-prompt | 4 +-- modules/bind-mount/bind-mount.sh | 24 ----------------- modules/sshfs/sshfs.sh | 31 ---------------------- modules/usage | 6 ----- modules/utility/machine.sh | 40 ---------------------------- modules/utility/mvlnbk.sh | 45 ++++++++++++++++++++++++++++++++ modules/utility/path.sh | 11 ++++++++ setup/.bash_profile | 6 +++++ setup/.bashrc | 7 +++++ setup/02-root.sh | 6 +++++ setup/03-bash.sh | 20 ++++++++++++++ setup/bash.bashrc | 27 +++++++++++++++++++ setup/profile | 37 ++++++++++++++++++++++++++ source-dir.func | 2 ++ 26 files changed, 254 insertions(+), 120 deletions(-) create mode 100644 .gitignore delete mode 100644 app/pio delete mode 100644 env/ssh.env create mode 100644 function/01-path delete mode 100644 function/ssh create mode 100644 function/startup rename setup.sh => load.sh (81%) delete mode 100755 modules/bind-mount/bind-mount.sh delete mode 100755 modules/sshfs/sshfs.sh delete mode 100644 modules/usage delete mode 100644 modules/utility/machine.sh create mode 100755 modules/utility/mvlnbk.sh create mode 100644 modules/utility/path.sh create mode 100644 setup/.bash_profile create mode 100644 setup/.bashrc create mode 100644 setup/02-root.sh create mode 100644 setup/03-bash.sh create mode 100644 setup/bash.bashrc create mode 100644 setup/profile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6384a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/archive/* diff --git a/README.md b/README.md index 8f9023a..7ab2992 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,7 @@ If you are really done with something but want to keep a copy in the repo move i if sourcing some file depends on another preface with numbers to put it before. Functions are all sourced before aliases so can be refered to there. Be aware that an alias or function set in the repo could be subsuming some existing binary or script. + + +`[[ $BASHPID -eq $$ ]] && echo was called directly || echo was called in a subshell` +https://unix.stackexchange.com/a/594809/201387 diff --git a/app/pio b/app/pio deleted file mode 100644 index 1414b00..0000000 --- a/app/pio +++ /dev/null @@ -1,4 +0,0 @@ -# pio in path and completion for platformio subcommands -export PATH=$PATH:~/.platformio/penv/bin -eval "$(_PLATFORMIO_COMPLETE=source platformio)" -eval "$(_PIO_COMPLETE=source pio)" diff --git a/env/01-opt.path b/env/01-opt.path index 0f4ee81..dd63931 100644 --- a/env/01-opt.path +++ b/env/01-opt.path @@ -1,4 +1,4 @@ -# set PATH so it includes user's private bin if it exists +# include binaries in /optu if [ -d "/opt/bin" ] ; then PATH="/opt/bin:$PATH" fi diff --git a/env/ssh.env b/env/ssh.env deleted file mode 100644 index 16f7c0b..0000000 --- a/env/ssh.env +++ /dev/null @@ -1,2 +0,0 @@ -# set ssh agent socket for each session -export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket" diff --git a/function/01-path b/function/01-path new file mode 100644 index 0000000..0208ac1 --- /dev/null +++ b/function/01-path @@ -0,0 +1,24 @@ +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/helpers b/function/helpers index d66c16b..ef4ac68 100644 --- a/function/helpers +++ b/function/helpers @@ -33,6 +33,11 @@ get_prop_value () { 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 diff --git a/function/logit b/function/logit index 50465fb..c643f35 100644 --- a/function/logit +++ b/function/logit @@ -17,3 +17,13 @@ function logit(){ echo $(date) logging for $SCRIPT_PATH fi } + +# +# LOG_DIR=/opt/bash/logs +# mkdir -p $LOG_DIR +# LOG_FILE=$LOG_DIR/bash_profile +# +# msg="$(date) \n Login for $USER \n $(env | grep BASH)" +# [[ $- == *i* ]] && echo -e $msg || echo -e $msg 1> $LOG_FILE 2>&1 +# +# diff --git a/function/modules b/function/modules index a747412..16ba7e8 100644 --- a/function/modules +++ b/function/modules @@ -32,6 +32,7 @@ local MDIRS local MDIR local DIRS local MODULE=$1 +# Precidence is user. current hosts, all hosts, current network, all networks, base DIRS=( \ $([ $BASH_SHELL_USER ] && echo $HOME/$BASH_SHELL_USER || echo $HOME/bash/shell) \ $BASH_SHELL_HOST/$(hostname) @@ -66,12 +67,14 @@ for MDIR in "${MDIRS[@]}" module_load () { [ ! $1 ] && echo "no module specified" && return 1 +# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed" local FILE FILE=$(module_find $1) [ $? -ne 0 ] && echo no module $1 found && return 1 # source $FILE "$0" +[[ $BASHPID -eq $$ ]] || echo $FILE source $FILE -echo $FILE + return 0 } diff --git a/function/ssh b/function/ssh deleted file mode 100644 index 262178a..0000000 --- a/function/ssh +++ /dev/null @@ -1,4 +0,0 @@ -# remote start a program (with x11 forwarding will render locally if gui) -function rrem(){ - ssh -X -t "$1" """$2" "$3""" -} diff --git a/function/startup b/function/startup new file mode 100644 index 0000000..4b69328 --- /dev/null +++ b/function/startup @@ -0,0 +1,35 @@ +#!/bin/bash +function startup_load () { + +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 \ +) + +[[ $1 ]] && DIRS=("$1") +echo startup ${DIRS[@]} +SDIRS=() +j=0 +cnt=${#DIRS[@]} +for ((i=0;i ' + +# Show usage and exit with status +show_usage_and_exit () { + echo $USAGE_STRING + exit 1 +} + +# ERROR file does not exist +no_file () { + echo $SCRIPT_NAME': '$1': No such file or directory' + exit 2 +} + +# Check syntax +if [ $# -ne 2 ]; then + show_usage_and_exit +fi + +# Check file existence +if [ ! -e "$1" ]; then + no_file $1 +fi + +# Get paths +source_path=$1 +destination_path=$2 + +# Check that destination ends with a slash +[[ $destination_path != */ ]] && destination_path="$destination_path"/ + +# Move source +[[ -d "$destination_path" ]] || mkdir -p "$destination_path" +mv "$source_path" "$destination_path" + +# Get original path +original_path=$destination_path$(basename $source_path) + +# Create symlink in source dir +ln -s "$original_path" "${source_path%/}" diff --git a/modules/utility/path.sh b/modules/utility/path.sh new file mode 100644 index 0000000..69cc9d0 --- /dev/null +++ b/modules/utility/path.sh @@ -0,0 +1,11 @@ +#!/bin/bash +function abs-path { + local target="$1" + if [ "$target" == "." ]; then + echo "$(pwd)" + elif [ "$target" == ".." ]; then + echo "$(dirname "$(pwd)")" + else + echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" + fi +} diff --git a/setup/.bash_profile b/setup/.bash_profile new file mode 100644 index 0000000..db35425 --- /dev/null +++ b/setup/.bash_profile @@ -0,0 +1,6 @@ +#!/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/.bashrc b/setup/.bashrc new file mode 100644 index 0000000..d062c82 --- /dev/null +++ b/setup/.bashrc @@ -0,0 +1,7 @@ +#!/bin/bash +echo "$USER .bashrc" + +# processing user's shell directory +[[ $BASH_SHELL_BASE_SOURCED = true ]] && \ +BASH_SHELL_USER=${BASH_SHELL_USER:-"bash/shell"} && \ +shell_process_directory "$HOME/$BASH_SHELL_USER" diff --git a/setup/02-root.sh b/setup/02-root.sh new file mode 100644 index 0000000..65ca4a4 --- /dev/null +++ b/setup/02-root.sh @@ -0,0 +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 + unset HISTFILE +fi diff --git a/setup/03-bash.sh b/setup/03-bash.sh new file mode 100644 index 0000000..2f8bec6 --- /dev/null +++ b/setup/03-bash.sh @@ -0,0 +1,20 @@ +# 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 new file mode 100644 index 0000000..0422303 --- /dev/null +++ b/setup/bash.bashrc @@ -0,0 +1,27 @@ +#!/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/profile b/setup/profile new file mode 100644 index 0000000..796051b --- /dev/null +++ b/setup/profile @@ -0,0 +1,37 @@ +#!/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/source-dir.func b/source-dir.func index 265ae83..06f73b3 100644 --- a/source-dir.func +++ b/source-dir.func @@ -67,6 +67,8 @@ fi local FIND FIND="find $DIR" FIND+=$([ ! $DEPTH == 0 ] && echo " -maxdepth $DEPTH ") +# ignore all .name and .path by default +# TODO change to having a string FIND+=" -type f ! -path \"*/.*/*\" ! -name \".*\" "