So I expect that this blog will contain a lot of moaning about the sorry state of production computing, of systems administration and of the world in general. But the reason that I'm in this field is because it's way too much fun. Specifically, I fell in love with GNU/Linux and The UN*X Way, and when it turned out that I could actually get paid to fiddle with this stuff… well, you can guess the story.
I get incredibly annoyed when I start some random terminal emulator (e.g. Emacs's own terminal-mode) and it doesn't accept ANSI color escape sequences. So here's a Boolean variable that I test for when setting various color options:
1: use_color=false
2: safe_term=${TERM//[^[:alnum:]]/.} # sanitize TERM
3:
4: # A good guess of whether this $TERM can display color
5: if [[ -f /etc/DIR_COLORS ]] ; then
6: grep -q "^TERM ${safe_term}" /etc/DIR_COLORS && use_color=true
7: elif type -p dircolors >/dev/null ; then
8: if dircolors --print-database | grep -q "^TERM ${safe_term}" ; then
9: use_color=true
10: fi
11: fi
I use a colorized prompt, but I couldn't decide which color I liked best – so I randomize it. I also have a little function that alerts me if the previous command exited with non-zero (i.e. abnormal) status:
12: function returncode {
13: returncode=$?
14: if [ $returncode != 0 ]; then
15: echo "[$returncode]"
16: else
17: echo ""
18: fi
19: }
20: if ${use_color} ; then
21: COLORS=(30 32 33 34 35 36 37)
22: n=$(($RANDOM % ${#COLORS[*]}))
23: b=$(($RANDOM % 2))
24: if [[ ${EUID} == 0 ]] ; then
25: PS1='\[\033[01;31m\]$(returncode)\[\033[01;31m\]\h \[\033[01;34m\]\W \$ \[\033[00m\]'
26: else
27: PS1='\[\033[${b};31m\]$(returncode)'"\[\033[01;${COLORS[n]}m\]\u@\h \[\033[01;34m\]\w \$ \[\033[00m\]"
28: fi
29: else
30: if [[ ${EUID} == 0 ]] ; then
31: # show root@ when we don't have colors
32: PS1='$(returncode)\u@\h \W \$ '
33: else
34: PS1='$(returncode)\u@\h \w \$ '
35: fi
36: fi
``less'' can do a lot more1 than you think. Here I set environment variables so that manpages are in color, and alias my invocation of less so that it can interpret ANSI color escape sequences – so now ``ls –color | less'' doesn't spam you with garbage… and the color option to grep works as well:
37: export LESS_TERMCAP_mb=$'\e[01;31m'
38: export LESS_TERMCAP_md=$'\e[01;31m'
39: export LESS_TERMCAP_me=$'\e[0m'
40: export LESS_TERMCAP_so=$'\e[01;44;33m'
41: export LESS_TERMCAP_se=$'\e[0m'
42: export LESS_TERMCAP_us=$'\e[01;32m'
43: export LESS_TERMCAP_ue=$'\e[0m'
44:
45: export PAGER="less -R"
46: alias less='less -R' # better than -r
Wait, you didn't know there was a –color option to grep!?
47: if ${use_color}; then
48: export GREP_COLOR='01;33'
49: alias grep='grep --color=yes'
50: fi
My first-ever blog post showed how to do this; I'm including it here for completeness' sake: here's how to slow down your computer when compiling:
51: if [ -a /proc/cpuinfo ]; then
52: export CONCURRENCY_LEVEL=$(($(grep -c processor /proc/cpuinfo) * 2 + 1))
53: export MAKEOPTS="-j${CONCURRENCY_LEVEL}"
54: fi
And last but not least, here are some handy aliases:
alias ipgrep='egrep -o "(([0-9]{1,3}\.){3}[0-9]{1,3})"' # iptables -L|ipgrep
alias rot13='tr A-Za-z N-ZA-Mn-za-m' # sbe terng whfgvpr
alias cg='egrep -v "^($|[[:space:]]*#|;)" ' # strip out comments
I do want to give credit to the authors of one of the best UN*X books I've ever read: Unix Power Tools from O'Reilly. Not only did this book teach me lots of *fu, but it advocates this very culture of hacking, documenting & sharing .bashrc. I recommend it to almost everybody that I know who expresses an interest in using UN*X better; it's great stuff.