guest@dotshare [~/groups/shells/bash] $ ls bashrc/ | cat

.bashrc (scrot) (+5 likes)

joh6nn Aug 05, 2011 (shells/bash)

.bashrc(raw, dl)

SCROT

1
2
3
4
5
6
7
8
9
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# added 2006-07-09
if [ -d ${HOME}/.bashrc.d ]; then
    for file in ${HOME}/.bashrc.d/*; do
        source "$file";
    done
fi

CLICK TO VIEW

x

.bashrc.d_prompt(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

source colors

function color_hash() {
	#shamelessly lifted from https://gist.github.com/377513
	#slightly modified from the original
	hash_word=$1
	num_of_colors=$2

	Color=0
	while [[ " 0 1 13 16 52 88 124 160 196 232 233 234 " == *\ ${Color}\ *  ]]; do
		hash_word=$(echo $hash_word | cksum)
		hash_word=${hash_word%% *}
		Color=$(( ${hash_word} % $num_of_colors ))
	done
	echo $Color
}

function utime() {
	uptime | egrep -o -e "up [0-9]*.*[0-9]* user[s]?" | tr -s ' '
}

function screens_count() {
	#doesn't catch named sessions, need to update
	echo -n `screen -ls | egrep -c "[0-9]+\.([a-zA-Z0-9\-]+)?\.[a-zA-Z]*"`
}


function disk_usage() {
	#pipe into awk '{print $3 }' for quantity used, $4 for quantity free, and $5 for percentage used
	df -h "`pwd`" | awk 'NR==2 { print $4 }'
}

function jobs_count() {
	echo -n `jobs | egrep -c \[[:digit:]+\]`
}

function screens_and_jobs_count() {
#	set -x
	all_jobs=`jobs_count`
	all_screens=`screens_count`
	results=""

	[ 0 -lt $all_screens ] && [ 0 -lt $all_jobs ] && echo -n " $all_screens screens, $all_jobs jobs |" && return
	[ 0 -lt $all_screens ] && [ 0 -eq $all_jobs ] && echo -n " $all_screens screens |" && return
	[ 0 -eq $all_screens ] && [ 0 -lt $all_jobs ] && echo -n " $all_jobs jobs |" && return
#	set +x
}

function free_memory() {
	echo -n `free -m | awk 'NR==3 { print $4"M" }'`
}

function laptop_battery() {
	if [ `hostname` = "hugin" ]; then
		on_ac_power || echo " `battmon2 -q` |"
	fi
}

if [ `id -u` -eq 0 ]; then
	PromptColor=${RedBG}${White}	#red background for root
else
	case $TERM in
		xterm*|screen*)
			PromptColor=$(color_hash $HOSTNAME 255)
			UserColor=$(color_hash $USER 255)
			x=x
		;;
		linux*)
			PromptColor=$(color_hash $HOSTNAME 8)
			UserColor=$(color_hash $USER 8)
		;;
	esac

	PromptColor="$(${x}bgcolor ${PromptColor})$(${x}fgcolor 0)"
	UserColor="${Normal}$(${x}fgcolor ${UserColor})"
fi

PS1=''

export PS1='${PromptColor}`date +"%a %F, %T"` | `utime` |`screens_and_jobs_count` `disk_usage` | `free_memory` |`laptop_battery` ${UserColor}${USER}${PromptColor}@${HOSTNAME}:${PWD/$HOME/~}${Normal}\n> '
 

x

.bashrc.d_ssh-screen(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if [ "$SSH_CONNECTION" ]; then  #we're in a ssh connection
        if [ -z "$STY" ]; then  #Screen is not currently running
		screen_opts=""
		screen_list=$(screen -ls | head -n -2 -) #get list of screen sessions
                if [ `echo -e "$screen_list" | egrep -c "[0-9]+\.([a-zA-Z0-9\-]+)?\.[a-zA-Z]*"` -gt 0 ]; then
			echo -e "\n$screen_list" | head -n 2 - 
			screen_list=$(echo -e "$screen_list" | tail -n +2)
			echo -e "\n$screen_list" | awk 'NR>1 { LINE+=1; printf "\t%d%s\n",LINE,$0; }'
			echo
                        # prompt, timeout a/f 10s, read only 1 char, default to most recent session
			read -n 1 -t 10 -p "Pick a # to reconnect to, or N to start new session: "
                        if [[ "n" = "$REPLY" ]]; then
                               	screen_opts=""
			elif [ -z "$REPLY" ]; then
				screen_opts="-D -r"
                        else
				REPLY=$(echo -e "$screen_list" | awk -v REPLY=$REPLY 'NR==REPLY { print $1}' )
                                screen_opts="-D -r $REPLY"
                        fi
                fi
                exec screen $screen_opts
        fi
fi
 

x

.bashrc.d_variables(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# set PATH so it includes user's private bin if it exists
if [[ -d ${HOME}/bin && ${PATH} != *${HOME}/bin* ]] ; then
        export PATH=${HOME}/bin:"${PATH}:"
fi

#added 2006-01-31 11:31 pm
export HISTCONTROL="erasedups:ignoreboth"		#erase duplicated entries, ignore entries that are duplicates or begin with spaces
export HISTTIMEFORMAT="%a %F %r "	
export HISTIGNORE="&:[ ]*:clear:exit"			#don't record commands preceeded by a space
export PROMPT_COMMAND="history -a; history -r"	#unify history across bash sessions

#added 2007-08-21 4:47 PM
export VERSION_CONTROL=numbered

#added 2009-03-05 00:08 am
export PAGER="/usr/bin/less -R"
export EDITOR=/usr/bin/vim

export LC_TIME=en_DK.UTF-8

#added 2010-09-25
export TERM=xterm-256color
 

x

.bashrc.d_colors(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /bin/bash

Normal="$(tput sgr0)"
Bold="$(tput op)"
Black="$(tput setaf 0)"
BlackBG="$(tput setab 0)"
DarkGrey="$(tput bold ; tput setaf 0)"
Red="$(tput setaf 1)"
RedBG="$(tput setab 1)"
LightRed="$(tput bold ; tput setaf 1)"
Green="$(tput setaf 2)"
GreenBG="$(tput setab 2)"
LightGreen="$(tput bold ; tput setaf 2)"
Brown="$(tput setaf 3)"
BrownBG="$(tput setab 3)"
Yellow="$(tput bold ; tput setaf 3)"
Blue="$(tput setaf 4)"
BlueBG="$(tput setab 4)"
LightBlue="$(tput bold ; tput setaf 4)"
Purple="$(tput setaf 5)"
PurpleBG="$(tput setab 5)"
Pink="$(tput bold ; tput setaf 5)"
Cyan="$(tput setaf 6)"
CyanBG="$(tput setab 6)"
LightCyan="$(tput bold ; tput setaf 6)"
LightGrey="$(tput setaf 7)"
LightGreyBG="$(tput setab 7)"
White="$(tput bold ; tput setaf 7)"

fgcolor() {
	[ $# -gt 1 ] && return
	echo -e "$(tput setaf $1)"
}

bgcolor() {
	[ $# -gt 1 ] && return
	echo -e "$(tput setab $1)"
}

xfgcolor() {
	[ $# -gt 1 ] && return
	echo -e "\e[\033[38;5;${1}m"
}

xbgcolor() {
	[ $# -gt 1 ] && return
	echo -e "\e[\033[48;5;${1}m"
}
 

x

.bashrc.d_functions(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
mdcd() {
	mkdir -p "$@" && cd "$@"
}

rmalias() {
        unalias $1 && sed -i "/alias $1\=/d" ~/.bashrc.d/aliases
}

mkalias() {
	echo -e "\n# added `date "+%F %T"`" >>  ${HOME}/.bashrc.d/aliases
	echo "alias $@" >> ${HOME}/.bashrc.d/aliases
	alias $@
}

###
###	Handy Extract Program
###     found at http://dotfiles.org/~blackbook/.bashrc
###	added 2008-10-07
extract () {
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xvjf $1    ;;
             *.tar.gz)    tar xvzf $1    ;;
             *.bz2)       bunzip2 $1     ;;
             *.rar)       unrar x $1     ;;
             *.gz)        gunzip $1      ;;
             *.tar)       tar xvf $1     ;;
             *.tbz2)      tar xvjf $1    ;;
             *.tgz)       tar xvzf $1    ;;
             *.zip)       unzip $1       ;;
             *.Z)         uncompress $1  ;;
             *.7z)        7z x $1        ;;
             *)           echo "'$1' cannot be extracted via >extract<" ;;
         esac
     else
         echo "'$1' is not a valid file"
     fi
}
 

x

.bashrc.d_misc(raw, dl)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# append to the history file instead of overwriting it
shopt -s histappend

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

if [ -f /etc/bash_completion ]; then
	. /etc/bash_completion
fi
 

x

Notes

a lot of this probably wildly inefficient, and some of it is just plain ugly, but it works, and it does what i want (mostly). criticism and suggestions welcome.

Comments

madznk said about 13 years ago

Awesome !