diff --git a/modules/editing/replace/replace.sh.save b/modules/editing/replace/replace.sh.save deleted file mode 100644 index cdbb08f..0000000 --- a/modules/editing/replace/replace.sh.save +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -function replace { - local PATTERN=$1; shift - local NEW_LINE=$1; shift - local FILE=$1; shift - [ ! "$PATTERN" ] || [ ! $FILE ] && return 1 - local BEFORE=$1 - local NEW=$(echo "${NEW_LINE}" | sed 's/\//\\\//g') - echo finding: $PATTERN Replace with $NEW before $BEFORE in $FILE - touch "${FILE}" || return 1 - echo matching: $PATTERN - match=false - if [ "$NEW_LINE" ]; then - sed -i '/'"${PATTERN}"'/{s/.*/'"${NEW}"'/;h};${x;/./{x;q100};{x;q20}}' "${FILE}" - [[ $? -eq 100 ]] && match=true - echo match $match - if [ $match == false ]; then - echo adding new entry $NEW_LINE - if [ "$BEFORE" ]; then - echo searching for $BEFORE to add line before - # sed -i '/'"${BEFORE}"'/{i/.*/'"${NEW}"'/;h};${x;/./{x;q100};x}' "${FILE}" - sed -i '/'"${BEFORE}"'/!{q20};/'"${BEFORE}"'/i\'"${NEW}" "${FILE}" - echo return $? - exit - # ;/i '"${NEW}"'' "${FILE}" - [[ $? -eq 100 ]] && match=true || match=false - echo before match $match - fi - # if [ $match == false ]; then - # echo appending to file - # echo "${NEW_LINE}" >> "${FILE}" - # fi - else echo line matched/replaced - fi - else - sed '/'"${PATTERN}"'/d' "${FILE}" - echo line removed - fi -} diff --git a/modules/network-dirs.sh b/modules/network-dirs.sh new file mode 100644 index 0000000..7cfde7b --- /dev/null +++ b/modules/network-dirs.sh @@ -0,0 +1,40 @@ +# to be sourced (below) not run in subshell! +function network_dirs () { + + (return 0 2>/dev/null) || (echo network_dirs must be sourced; return 1) + + module_load path + [[ $? -ne 0 ]] && echo unable to access the file module, aboarting load && return 1 + + + local network + [[ ! -f $1 ]] && return + + while read -r network || [ -n "$network" ] + do + if [[ ${network} = *!* ]]; then + network=${network/!/} + NETWORK_HOME=$network + fi + [[ $2 ]] && network=$2/$network + if [[ $(isAbsPath $network) ]]; then + [[ ! -d $network ]] && continue + else + if [[ -d ${BASH_SHELL_USER}/${network} ]];then + network=${BASH_SHELL_USER}/${network} + else + if [[ -d ${BASH_NETWORKS_DIR}/${network} ]];then + network=${BASH_NETWORKS_DIR}/${network} + else + continue + fi + fi + fi + [[ $network =~ $NETWORK_HOME ]] && NETWORK_HOME_DIR=${network} + BASH_SHELL_NETWORK_DIRS+="${network} " + BASH_SHELL_DIRS+="${network} " + done < "$1" + export NETWORK_HOME + export NETWORK_HOME_DIR + export BASH_SHELL_NETWORK_DIRS +} \ No newline at end of file diff --git a/modules/shell-process-directory.sh b/modules/shell-process-directory.sh new file mode 100644 index 0000000..95a9016 --- /dev/null +++ b/modules/shell-process-directory.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +function shell_process_directory () { + local SUBDIRS + local DIR + + module_load file + [[ $? -ne 0 ]] && echo unable to access the file module, aborting load && return 1 + + + DIR=${1:-$BASH_SHELL_BASE} + # echo soucring directory $DIR + # if [[ $DIR = "$BASH_SHELL_BASE" ]]; then + # BASH_SHELL_IGNORE=$(shell_get_ignores) + # excludes=$BASH_SHELL_IGNORE + # else + # excludes=$(shell_get_ignores $DIR) + # [[ $? -ne 0 ]] && excludes=$BASH_SHELL_IGNORE + # fi + + if [ -d "$DIR" ]; 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 + echo + [[ ! $REPLY =~ ^[Yy]$ ]] && return 1 + # else + # echo non-interactive shell + # return 1 + fi + fi + + if [[ $DIR != "$BASH_SHELL_BASE" ]] && [[ -f $DIR/load.sh ]]; then + # echo "$DIR/load.sh" found, running instead of default load + source "$DIR/load.sh" + else + # default processing + local SUBDIRS + local IGNORE_FILE + 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" + 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 + fi + # echo "done sourcing directory $DIR" + # else + # echo $DIR does not exist nothing to source + fi + + } + diff --git a/modules/utility/user.sh b/modules/utility/user.sh new file mode 100755 index 0000000..6ad1d54 --- /dev/null +++ b/modules/utility/user.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +# clone a user + +function clone_user_ () { + +module_load confirm +type confirm + +echo "=============" +echo "this script will create a new user" +echo "based on an existing user's data" +echo +echo "You will be shown a list of users who can currently log on" +echo "Remember which user you would like to clone." +echo "You will be asked for the new user's name, their password" +echo "and the old user to clone". +echo "=============" +echo + +echo -n "New user's name: " +read newuser + +echo -n "New user's password: " +read newpassword + +echo + +echo "Current users you can clone:" +echo "----" +awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd +echo + +echo -n "Old user to clone: " +read olduser +echo "olduser uid is $(id -u $olduser)" + +echo +echo "You have selected: " +echo "----" +echo "new user: $newuser" +echo "new user password: $newpassword" +echo "old user: $olduser" +echo + +olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser" +olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd) + +echo "old user groups: " +echo "----" +echo $olduser_GROUPS +echo "olduser shell: " +echo $olduser_SHELL +# read -rsp $'Press any key to continue or ctrl-c to exit...\n' -n1 key +# return +# confirm "ready to clone user, begin?" || return 1 + +# useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser + +# echo $newuser:$newpassword | chpasswd + +# read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key + +# mkdir /home/$newuser +# chown -R $newuser:$newuser /home/$newuser + +# echo +# echo "Script should be done now." +# echo +# echo "Do you see your new users name below?" +# echo +# awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd + +# echo +# echo "We are now going to copy the old user's home folder to the new user" +# echo "then change ownership to the new user" +# echo +# read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key + + +rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/ +chown -R --from=$olduser $newuser:$newuser /home/$newuser + +echo +echo "Now we are going to change the names of files and folders to the new user" +echo + +grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g' + +echo +echo "Done now." +echo +read -rsp $'Press any key to exit...\n' -n1 key +echo +echo + +} + +clone_user () { +if [[ $EUID != 0 ]]; then + + clone=$(declare -f clone_user_) + module_load confirm + confirm=$(declare -f confirm) + sudo bash -c "$confirm; $clone; clone_user_" + else + echo run as regular user with sudo privliges and it will elevate +fi +} + +# # if script was executed then call the function +(return 0 2>/dev/null) || clone_user \ No newline at end of file diff --git a/modules/utility/watch.sh b/modules/utility/watch.sh new file mode 100755 index 0000000..a106dbd --- /dev/null +++ b/modules/utility/watch.sh @@ -0,0 +1,113 @@ +#!/bin/bash +# (c) Wolfgang Ziegler // fago +# +# Inotify script to trigger a command on file changes. +# +# The script triggers the command as soon as a file event occurs. Events +# occurring during command execution are aggregated and trigger a single command +# execution only. +# +# Usage example: Trigger rsync for synchronizing file changes. +# ./watch.sh rsync -Cra --out-format='[%t]--%n' --delete SOURCE TARGET +# +# ./watch.sh rsync -Cra --out-format='[%t]--%n' --include core \ +# --WATCH_EXCLUDE=sites/default/files --delete ../web/ vagrant@d8.local:/var/www + +######### Configuration ######### + +watch() { + +EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO" +COMMAND="$@" + +# echo $COMMAND $WATCH_DIR, $WATCH_VERBOSE +## The directory to watch. +if [ -z "WATCH_DIR" ]; then + WATCH_DIR=. +fi + +## WATCH_EXCLUDE Git and temporary files from PHPstorm from watching. +if [ -z "$WATCH_EXCLUDE" ]; then + WATCH_EXCLUDE='(\.git|___jb_)' +fi + +## Whether to enable verbosity. If enabled, change events are output. +if [ -z "WATCH_VERBOSE" ]; then + WATCH_VERBOSE=0 +fi + +################################## + +if [ -z "$1" ]; then + echo "Usage: $0 Command" + exit 1; +fi + +## +## Setup pipes. For usage with read we need to assign them to file descriptors. +## +RUN=$(mktemp -u) +mkfifo "$RUN" +exec 3<>$RUN + +RESULT=$(mktemp -u) +mkfifo "$RESULT" +exec 4<>$RESULT + +clean_up () { + ## Cleanup pipes. + rm $RUN + rm $RESULT +} + +## Execute "clean_up" on exit. +trap "clean_up" EXIT + + +## +## Run inotifywait in a loop that is not blocked on command execution and ignore +## irrelevant events. +## + +inotifywait -m -q -r -e $EVENTS --exclude $WATCH_EXCLUDE --format '%w%f' $WATCH_DIR | \ + while read FILE + do + if [ $WATCH_VERBOSE -ne 0 ]; then + echo [CHANGE] $FILE + fi + + ## Clear $PID if the last command has finished. + if [ ! -z "$PID" ] && ( ! ps -p $PID > /dev/null ); then + PID="" + fi + + ## If no command is being executed, execute one. + ## Else, wait for the command to finish and then execute again. + if [ -z "$PID" ]; then + ## Execute the following as background process. + ## It runs the command once and repeats if we tell him so. + ($COMMAND; while read -t0.001 -u3 LINE; do + echo running >&4 + $COMMAND + done)& + + PID=$! + WAITING=0 + else + ## If a previous waiting command has been executed, reset the variable. + if [ $WAITING -eq 1 ] && read -t0.001 -u4; then + WAITING=0 + fi + + ## Tell the subprocess to execute the command again if it is not waiting + ## for repeated execution already. + if [ $WAITING -eq 0 ]; then + echo "run" >&3 + WAITING=1 + fi + + ## If we are already waiting, there is nothing todo. + fi +done + +} \ No newline at end of file