From ce22eb7339e04e6ee2c2c2c11d78f701b436d99b Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 23 Mar 2022 16:04:39 +0300 Subject: [PATCH] initial commit... Signed-off-by: Alex A. Naanou --- .gitignore | 2 + bashctrl | 200 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/alias | 52 ++++++++++++ plugins/bindings | 33 ++++++++ plugins/cd_func | 58 ++++++++++++++ plugins/env | 1 + plugins/fortune | 4 + plugins/prompt | 40 ++++++++++ plugins/ssh-agent | 26 ++++++ 9 files changed, 416 insertions(+) create mode 100644 .gitignore create mode 100644 bashctrl create mode 100644 plugins/alias create mode 100644 plugins/bindings create mode 100644 plugins/cd_func create mode 100644 plugins/env create mode 100644 plugins/fortune create mode 100644 plugins/prompt create mode 100644 plugins/ssh-agent diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9540792 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.sw[po] +.gitignore diff --git a/bashctrl b/bashctrl new file mode 100644 index 0000000..ef75348 --- /dev/null +++ b/bashctrl @@ -0,0 +1,200 @@ +#!/bin/bash + +SCRIPT=`basename $0` +ORIGPATH=`pwd` + +printhelp () { + echo 'Bash Control script' + echo 'format:' + echo ' bashctrl COMMAND [ARGS]' + echo + echo 'supported commands:' + echo ' help - print this message.' + echo ' list - list events and installed plugins.' + echo ' script - list the plugins in a runnable bash script format.' + echo ' plugins - list available plugins.' + echo ' events - list supported events.' + echo ' add PLUGIN PRIO EVT' + echo ' - install PLUGIN to EVT with PRIO.' + echo ' if a plugin is already installed, change its settings.' + echo ' NOTE: use a two digit priority (arbitary digit prios' + echo ' are not supported by some commands).' + echo ' del PLUGIN EVT|all' + echo ' - remove PLUGIN from EVT.' + echo ' if "all" is given, remove from all events.' + # XXX + #echo ' setup - setup bashctrl directories.' + #echo ' update - update pligins' + #echo ' run EVENT' + #echo ' - run event scripts' + echo + echo 'other commands:' + echo ' drop-all-plugin-bindings' + echo ' - removes all installed plugin bindings.' +} + +if [ -z $1 ]; then + printhelp + exit +fi + +cd ~/.bash/ + +while true; do + if [ -z $1 ]; then + break + fi + case $1 in + -h|--help|help) + printhelp + break + ;; + --add-plugin|add) + if [[ $# < 4 ]] ; then + echo "$SCRIPT: Error: wrong argument format." + exit 1 + fi + # TODO check if such a plugin exists... + file=events/${4}/${3}_${2} + # remove old version if it exists... + # TODO make this silent... + plugin=plugins/${2} + if [ -r $plugin ]; then + evt=`echo events/${4}/*_${2}` + for p in $evt; do + if [ $p == $evt ] && ! [ -e $p ]; then + break + fi + rm $p + done + # TODO test this... + ln -s ../../$plugin $file + else + echo "$SCRIPT: Error: plugin \"$2\" does not exist or is not readable." + fi + break + ;; + + --del-plugins|del) + if [ ${3} = all ] || [ ${3} = '' ] ; then + files=`echo events/*/*_${2}` + echo "rmoving \"$files\"..." + rm $files + else + files=`echo events/${3}/*_${2}` + echo "rmoving \"$files\"..." + rm $files + fi + break + ;; + + --events|events) + echo supported events: + for event in events/* ; do + if [ $event = "events/run" ]; then + continue + fi + echo " ${event/events\//}" + if [ -e ${event}/DOC ] ; then + cat ${event}/DOC + echo + fi + done + break + ;; + + --list|list) + echo installed plugins: + for event in events/* ; do + if [ $event = "events/run" ]; then + continue + fi + echo " ${event/events\//}" + for plugin in ${event}/* ; do + plugin=`basename $plugin` + if [[ $plugin == 'DOC' ]] ; then + continue + fi + echo " $plugin" + done + done + break + ;; + + --script|script) + echo '#!/bin/bash' + echo '# this script will setup a bash plugin configuration.' + echo '# generated by: bashctrl script' + echo '#' + echo '# if you want to do a clean install uncomment the next line...' + echo '##bashctrl drop-all-plugin-bindings' + for event in events/* ; do + if [ $event = "events/run" ]; then + continue + fi + echo + echo "# event: ${event/events\//}" + for plugin in ${event}/* ; do + plugin=`basename $plugin` + if [[ $plugin == 'DOC' ]] ; then + continue + fi + echo "bashctrl add ${plugin/??_/} ${plugin/_*/} ${event/events\//}" + done + done + echo + break + ;; + + --drop-all-plugin-bindings|drop-all-plugin-bindings) + read -p "are you sure you want to remove all current bash plugin bindings? (yes/No) " ans + if [[ $ans == 'yes' ]] ; then + rm -f ~/.bash/events/*/* + else + echo "not doing anything..." + fi + break + ;; + + + --list-plugins|plugins) + echo available plugins: + for plugin in plugins/* ; do + plugin=`basename $plugin` + if [ $plugin = "README" ]; then + continue + fi + echo " ${plugin}" + done + break + ;; + + + setup) + mkdir -p \ + ~/.bash/{commands,dat,local,plugins}/ \ + ~/.bash/events/{start,login,logout,end}/ + break + ;; + + + update) + break + ;; + + + run) + if [ -z $2 ] ; then + echo 'bashctrl: no EVENT given.' >&2 + exit 1 + fi + for plugin in events/$2/* ; do + source $plugin $2 + done + break + ;; + esac + shift +done + +cd $ORIGPATH diff --git a/plugins/alias b/plugins/alias new file mode 100644 index 0000000..fbdcca4 --- /dev/null +++ b/plugins/alias @@ -0,0 +1,52 @@ +#====================================================================== +# File: alias_man.sh +# Written By: Alex A. Naanou +#====================================================================== + +# start +if [ $1 = "start" ] || [ $1 = 'login' ] ; then + if [ -z $__ALIAS_RUN__ ]; then + __ALIAS_RUN__='yes' + + # internal + #alias management: + alias alias_save="alias > ~/.bash/alias_lst" + alias alias_save_bak="alias > ~/.bash/alias_lst.bak" + alias alias_clear="mv ~/.bash/alias_lst ~/.bash/alias_lst.bak" + alias alias_revert="cp ~/.bash/alias_lst.bak ~/.bash/alias_lst" + + # external + if ! source ~/.bash/alias_lst ; then + # defaults + echo "loading backup aliases..." + if ! source ~/.bash/alias_lst.bak ; then + echo "loading default aliases..." + alias l="ls --color=auto" + alias ll="ls --color=auto -l" + alias scp="printf \"\e]2;scp in progress at \${USER}@\${HOSTNAME} (\`pwd\`)\a\";scp" + alias talk="printf \"\e]2;talking at \${USER}@\${HOSTNAME} (\`pwd\`)\a\";talk" + alias cdrdao='printf "\e]2;cdrdao at \${USER}@${HOSTNAME} (\`pwd\`)\a";cdrdao' + alias cdrecord='printf "\e]2;cdrecord at \${USER}@${HOSTNAME} (\`pwd\`)\a";cdrecord' + alias_save + cp ~/.bash/alias_lst ~/.bash/alias_lst.bak + fi + fi + fi +fi +if [ $1 = "logout" ] || [ $1 = "end" ] ; then + if [ -z $__ALIAS_UNRUN__ ]; then + __ALIAS_UNRUN__='yes' + + if [[ $SAVE != "no" ]] ; then + echo "saving settings..." + alias_save + bind_save + else + echo "aborting save..." + sleep 1 + fi + fi +fi + +#=================================================================END== +true diff --git a/plugins/bindings b/plugins/bindings new file mode 100644 index 0000000..07a1ac8 --- /dev/null +++ b/plugins/bindings @@ -0,0 +1,33 @@ +#====================================================================== +# File: bindings.sh +# Written By: Alex A. Naanou +#====================================================================== + +#===============================================================START== +# internal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +#bind -x '"\C-b": SAVE="no";logout' + +#mapping management: +alias bind_save="bind -p > ~/.bash/bind_lst" +alias bind_save_bak="bind -p > ~/.bash/bind_lst.bak" +alias bind_clear="read -p 'This may render your shell unusable! are you shure? (yes/no): ' -n 4 answer;test $answer='yes'&&echo boo" +alias bind_revert="bind -f ~/.bash/bind_lst.bak;cp ~/.bash/bind_lst.bak ~/.bash/bind_lst" + + +# external - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +if [ -a ~/.bash/bind_lst ] ; then + bind -f ~/.bash/bind_lst +else + echo "loading backup bindings..." + if [ -a ~/.bash/bind_lst.bak ] ; then + bind -f ~/.bash/bind_lst.bak + else + echo "loading default bindings..." + # put your default bindings here... + # + cp ~/.bash/bind_lst ~/.bash/bind_lst.bak + fi +fi + +#=================================================================END== +true diff --git a/plugins/cd_func b/plugins/cd_func new file mode 100644 index 0000000..419a1e7 --- /dev/null +++ b/plugins/cd_func @@ -0,0 +1,58 @@ +# This function defines a 'cd' replacement function capable of keeping, +# displaying and accessing history of visited directories, up to 10 entries. +# To use it, uncomment it, source this file and try 'cd --'. +# acd_func 1.0.5, 10-nov-2004 +# Petar Marinov, http:/geocities.com/h2428, this is public domain +cd_func () +{ + local x2 the_new_dir adir index + local -i cnt + + if [[ $1 == "--" ]]; then + dirs -v + return 0 + fi + + the_new_dir=$1 + [[ -z $1 ]] && the_new_dir=$HOME + + if [[ ${the_new_dir:0:1} == '-' ]]; then + # + # Extract dir N from dirs + index=${the_new_dir:1} + [[ -z $index ]] && index=1 + adir=$(dirs +$index) + [[ -z $adir ]] && return 1 + the_new_dir=$adir + fi + + # + # '~' has to be substituted by ${HOME} + [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}" + + # + # Now change to the new dir and add to the top of the stack + pushd "${the_new_dir}" > /dev/null + [[ $? -ne 0 ]] && return 1 + the_new_dir=$(pwd) + + # + # Trim down everything beyond 11th entry + popd -n +11 2>/dev/null 1>/dev/null + + # + # Remove any other occurence of this dir, skipping the top of the stack + for ((cnt=1; cnt <= 10; cnt++)); do + x2=$(dirs +${cnt} 2>/dev/null) + [[ $? -ne 0 ]] && return 0 + [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}" + if [[ "${x2}" == "${the_new_dir}" ]]; then + popd -n +$cnt 2>/dev/null 1>/dev/null + cnt=cnt-1 + fi + done + + return 0 +} + +alias cd=cd_func diff --git a/plugins/env b/plugins/env new file mode 100644 index 0000000..9bc0717 --- /dev/null +++ b/plugins/env @@ -0,0 +1 @@ +export HISTCONTROL=ignoredups diff --git a/plugins/fortune b/plugins/fortune new file mode 100644 index 0000000..b5b6990 --- /dev/null +++ b/plugins/fortune @@ -0,0 +1,4 @@ +#!/bin/bash +if [[ $BASH_LOGIN == 1 ]] || [[ $BASH_PROFILE == 1 ]] ;then + [ -f ~/.greet ] && cat ~/.greet || fortune +fi diff --git a/plugins/prompt b/plugins/prompt new file mode 100644 index 0000000..27d1ddb --- /dev/null +++ b/plugins/prompt @@ -0,0 +1,40 @@ +#====================================================================== +# File: prompt.sh +# Written By: Alex A. Naanou +#====================================================================== + + +#===============================================================START== +# level indicator +if [[ $TERM == 'screen' ]]; then + __SCREEN__="s " + export __SCREEN__ +fi +if [[ $BASH_PROFILE == 1 ]] ; then + if [[ $SHLVL != 1 ]];then + if [[ -z $__LOGIN_PRESENT__ ]];then + __SHLVL__="($SHLVL)* " + else + __SHLVL__="($SHLVL*)* " + fi + export __LOGIN_PRESENT__="*" + fi + if [[ $TERM == xterm ]] ; then + PS1='\e]2;\u@\h (\w)\a\n\e[32m\u@\h \e[34m$__SHLVL__\e[33m\w\e[0m\n\$ ' + else + PS1='\n\e[32m\u@\h \e[34m$__SHLVL__\e[33m\w\e[0m\n\$ ' + fi +fi +if [[ $BASHRC == 1 ]] ; then + if [[ $SHLVL != 1 ]];then + __SHLVL__="($SHLVL$__LOGIN_PRESENT__) " + fi + if [[ $TERM == xterm ]] ; then + PS1='\e]2;\u@\h (\w)\a\n\e[32m\u@\h \e[34m$__SCREEN__$__SHLVL__\e[33m\w\e[0m\n\$ ' + else + PS1='\n\e[32m\u@\h \e[34m$__SCREEN__$__SHLVL__\e[33m\w\e[0m\n\$ ' + fi +fi + +#=================================================================END== +true diff --git a/plugins/ssh-agent b/plugins/ssh-agent new file mode 100644 index 0000000..8bed193 --- /dev/null +++ b/plugins/ssh-agent @@ -0,0 +1,26 @@ +#!/bin/bash +function run_sshagent() +{ + echo "alias echo=#" > ~/.bash/tmp/ssh-agent.tmp + ssh-agent >> ~/.bash/tmp/ssh-agent.tmp + echo "unalias echo" >> ~/.bash/tmp/ssh-agent.tmp + sleep 1 + ssh-add +} + +# check if an agent is running +if ! source ~/.bash/tmp/ssh-agent.tmp ; then + #echo initiating ssh-ahent + echo "starting ssh-agent... [${SSH_AGENT_PID}]" + run_sshagent + source ~/.bash/tmp/ssh-agent.tmp +else + ps_string=`ps | grep $SSH_AGENT_PID` + if [[ ${ps_string/*ssh-agent/ssh-agent} != 'ssh-agent' ]] ; then + #echo initiating ssh-ahent + echo "starting ssh-agent... [${SSH_AGENT_PID}]" + run_sshagent + source ~/.bash/tmp/ssh-agent.tmp + fi +fi +