Custom bash_profile for Advanced Shell Users

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Custom bash_profile for Advanced Shell Users

Looking for some advanced uses for the shell? Here is some of my best. Works for all shells I encounter, including BackTrack, Debian, Knoppix, Arch Linux, etc. Also works for many hosting environments I use including DreamHost, HostGator, WiredTree, and pretty much any linux VPS.

Want to get started? Type the following in your shell to download the script and run it. You should go over the code first and then you can just save it as your .bash_profile – whenever you want to get updated to the latest version just type aaup, a cool update function that auto dos2unix’s and runs via an exec command.

curl -O http://static.askapache.com/askapache-bash-profile.txt && source askapache-bash-profile.txt
Or if you trust me and my hosting provider (and you aren't on production)
curl -o ~/.bash_profile http://static.askapache.com/askapache-bash-profile.txt && exec bash -l

For those of you power users and server admins that use Bash, ksh, csh, vanilla sh, etc.., or if you are just passionate about shell scripting, because it allows you to get advanced tasks done fast and efficiently, not to mention automated automatically. I give you my .bash_profile file. You should edit it to fit your needs, (especially the exported vars like PATH, LDFLAGS, if you don’t understand something just comment it out) but it’s pretty universal because I work on alot of other people’s servers not to mention many different distros and platforms, and when I get hired to do some server work through a shell, I bring this script along for the ride.

askapache-bash-profile.txt

You can download the latest version: http://static.askapache.com/askapache-bash-profile.txt

The functions and variables below are the way bash sees them, using declare -f, and alias, to make it easier for you to read and understand them. The actual file at http://static.askapache.com/askapache-bash-profile.txt will always be the most updated version, as I use it personally. And it has the whole file the way I wrote it, meaning many extra notes and much simpler to follow. Enjoy!

Gist of the Script..

I also rely on this heavily from within shell scripts I write to access all the functions and stuff in this .bash_profile, and to do that I just do like:

#!/bin/bash
 
source ~/.bash_profile &>/dev/nulll
 
pm "PM is a function to output nice messages with color"
yn "Are you enjoying the shell" && pm "Thats great!" || pm "Perhaps you're better suited for DOS"
yn "Show Calendar" && aa_calendar
yn "Show Fortune" && aa_fortune
alias chmod='command chmod -c'
alias cpr='command cp -rpv'
alias df='command df -kTh'
alias df1='command df -iTa'
alias diff='diff -up'
alias dsiz='du -sk * | sort -n --'
alias du='command du -kh'
alias du1='echo *|tr " " "\n" |xargs -iFF command du -hs FF|sort'
alias env='command env | sort'
alias h='history'
alias inice='ionice -c3 -n7 nice'
alias j='jobs -l'
alias la='command ls -Al --color=auto'
alias lc='command ls -lAcr --color=auto'
alias less='vless'
alias lessc='ccze -A |`type -P less` -R'
alias lk='command ls -lASr --color=auto'
alias llh='ll -h'
alias lll='stat -c %a\ %N\ %G\ %U ${PWD}/*|sort'
alias lr='command ls -lAR --color=auto'
alias lt='command ls -lAtr --color=auto'
alias lu='command ls -lAur --color=auto'
alias lx='command ls -lAXB --color=auto'
alias mann='command man -H'
alias n='/usr/bin/nano3'
alias p='command ps -HAcl -F S -A f|uniq -w3'
alias path='echo -e ${PATH//:/\\n}'
alias php='php -d report_memleaks=1 -d report_zend_debug=1 -d log_errors=0 -d ignore_repeated_errors=0 -d ignore_repeated_source=0 -d error_reporting=30719 -d display_startup_errors=1 -d display_errors=1'
alias pp='command ps -HAcl -F S -A f'
alias ps1='command ps -lFA'
alias ps2='command ps -H'
alias resetw='echo $'\''\33[H\33[2J'\'''
alias subash='sudo sh -c '\''export HOME=/root; cd /root; exec bash -l'\'''
alias top='top -c'
alias tree='command tree -Csuflapi'
alias updatedb='( ( updatedb 2>/dev/null ) & )'
alias vim='command vim --noplugin'
alias who='command who -ar -pld'
alias wtf='watch -n 1 w -hs'
aa_aliases ()
{
    local F=`for i in $(alias|awk -F= '{print $1}');do echo $i;done|sed -e :a -e 's/^.\{1,15\}$/& /;ta'|tr ' ' '\032'`;
    i=1;
    for f in ${F};
    do
        echo -en "$f";
        (( $i % 4 ==0 )) && echo -en "\n";
        i=$(( $i + 1));
    done | tr '\032' " "
}
aa_beep ()
{
    local i;
    for i in `seq 1 ${1:-5}`;
    do
        echo -en "\a" && sleep 1;
    done
}
aa_calendar ()
{
    [[ -d /usr/share/calendar/ ]] && echo -en "\n${CC[4]}" && ( sed = $(echo /usr/share/calendar/calendar*) | sed -n "/$(date +%m\\/%d\\\|%b\*\ %d)/p" ) && echo -en "${R}"
}
aa_current_users ()
{
    echo -n "`command who -q |sed '/^# users=\([0-9]*\).*/!d; s//\1/;q'`"
}
aa_find_exec ()
{
    find -L ${1:-.} -type f -perm +0100 2>$N6 | xargs file | sed -e 's%^\([^:]*\):\([^,]*\).*$%\1:\2%g'
}
aa_fortune ()
{
    local FORTUNE=`command type -P fortune`;
    [[ -x $FORTUNE ]] && echo -en "\n${CC[5]}" && $FORTUNE -s && echo -en "${R}";
    [[ -x $FORTUNE ]] && echo -en "\n${CC[6]}" && $FORTUNE -s && echo -en "${R}";
    [[ -x $FORTUNE ]] && echo -en "\n${CC[7]}" && $FORTUNE -s && echo -en "${R}";
    [[ -x $FORTUNE ]] && echo -en "\n${CC[8]}" && $FORTUNE -s && echo -e "${R}"
}
aa_free_mem ()
{
    echo -n "`[[ -r /proc/meminfo ]] && sed '/^MemFree: */!d; s///;q' /proc/meminfo`"
}
aa_functions ()
{
    local F=`for i in $(declare -F | sed -e 's/declare -f //g'); do echo $i; done| sed -e :a -e 's/^.\{1,25\}$/& /;ta'|tr ' ' '\032'`;
    i=1;
    for f in ${F};
    do
        echo -en "$f";
        (( $i % 4 ==0 )) && echo -en "\n";
        i=$(( $i + 1));
    done | tr '\032' " "
}
aa_getsrc ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME <url>" 1>&2 && return 2;
    [[ -z "$CPPFLAGS" ]] && pm "Setting CPPFLAGS" && export CPPFLAGS="-I/usr/local/include/libxml2 -I/opt/libiconv/include";
    [[ -z "$LD_FLAGS" ]] && pm "Setting LD_FLAGS" && export LD_FLAGS="-L/usr/local/lib -L/usr/local/lib/python2.4/site-packages -L/usr/local/lib -lxml2 -lz -L/opt/libiconv/lib -liconv -lm";
    function aa_err ()
    {
        echo '';
        local s;
        echo -en "\n [ ${1:-Press any key to continue} ]\n";
        read -n 1 s
    };
    function aa_install ()
    {
        ./configure --help;
        ./configure --prefix=/usr/local "$*" && make -j3 && sudo make install || aa_err;
        echo -en "\a"
    };
    function aa_extract ()
    {
        case $1 in
            *.tar.bz2)
                tar xjf $1
            ;;
            *.tar.gz)
                tar xzf $1
            ;;
            *.tar)
                tar xf $1
            ;;
            *.tbz2)
                tar xjf $1
            ;;
            *.tgz)
                tar xzf $1
            ;;
            *.bz2)
                bunzip2 $1
            ;;
            *.rar)
                unrar x $1
            ;;
            *.gz)
                gunzip $1
            ;;
            *.zip)
                unzip $1
            ;;
            *.Z)
                uncompress $1
            ;;
            *.7z)
                7z x $1
            ;;
            *)
                aa_err
            ;;
        esac
    };
    local OD=`pwd -L`;
    local FN=$(echo $1 | sed -e 's/^.*\/\([^\/]\{3,\}\)$/\1/g');
    local SN=$(echo $FN |  sed -e 's/\(.\{1\}\)\([^-.]\{1,\}\).*/\1\2/g');
    echo -e "OD: $OD \n1: $1 \nFN: $FN \nSN: $SN" && sleep 1;
    cd /opt/dist;
    ( curl -# -L -f -b c.txt --cookie-jar c.txt -m 400 -O -H 'Accept: */*' -s -S -A 'Mozilla/5.0 (Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/2009 Firefox/3.5.5' -e "$1" --url "$1" || v_err "CURL FAILED for $1" );
    [[ -f "$FN" ]] && aa_extract "$FN" || aa_err "EXTRACTING FAILED for $FN, go manual it";
    [[ -d "/opt/$SN" ]] && mv -v /opt/$SN /opt/.old-$SN;
    mv -v /opt/source/`echo $1|sed -e 's/.*\/\(.*\)\.\([tgz7bar2v]*\)\(\.[tgz7bar2v]*\)$/\1/g'` /opt/$SN || aa_err "Please mv $1 to /opt/$SN";
    cd /opt/$SN && command ls -lABls1c $AAC && aa_install;
    cd $OD
}
aa_hexdump ()
{
    local EX;
    [[ $# -eq 1 ]] && EX=exec;
    [[ -f "$1" ]] && EX='cat "$1"';
    case ${2:-1} in
        0)
            $EX hexdump -C
        ;;
        1)
            $EX hexdump -Cc
        ;;
        2)
            $EX hexdump -Cx
        ;;
        3)
            $EX hexdump -C -e '""  10/1 "'\''%_c'\''\t" "\n"' -e '"" 10/1 "0x%02x\t" "\n\n"'
        ;;
        4)
            $EX hexdump -e '""  10/1 "'\''%_c'\''\t" "\n"'
        ;;
        5)
            $EX hexdump -e '""  10/1 "'\''%_c'\''\t" "\n"' -e '"" 10/1 "0x%04x\t" "\n\n"'
        ;;
        6)
            $EX hexdump -e '90/1 "%_p" "\n"'
        ;;
        7)
            $EX hexdump -e '1/1 "%04_u" "%4_p" "   " 1/1 "%04x" "  " 1/1 "%04_c"  "\n"'
        ;;
        8)
            $EX hexdump -v -e '"x" 1/1 "%02X" " "'
        ;;
        9)
            $EX od -Ax -tx1 -v
        ;;
        10)
            $EX od -t o1z -w1 -Ao -v
        ;;
        11)
            $EX xxd -
        ;;
        12)
            $EX xxd -c1
        ;;
        13)
            $EX xxd -h
        ;;
        14)
            $EX
        ;;
    esac
}
aa_ips ()
{
    ahave ip && ip -o -f inet addr 2>&1 | sed -e 's/.*inet \([^/]*\).*/\1/g' | sort -u;
    ahave ss && ss -n 2>&1 | sed -e 's/[^:]*\ \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)\:.*/\1/' -e '/^\([^0-9]\|127\.\|10\.\|172\.\)/d' | sort -u;
    [[ -f "$HOME/.cpanel/datastore/_sbin_ifconfig_-a" ]] && sed -e '/inet/!d' -e 's/.*addr:\([0-9\.]*\).*/\1/g' "$HOME/.cpanel/datastore/_sbin_ifconfig_-a" | sort -u;
    sed -e 's/^\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/' -e '/^\([^0-9]\|127\.\|10\.\|172\.\|$\)/d' /etc/hosts | sort -u
}
aa_isalpha ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME str" 1>&2 && return 2;
    case $1 in
        *[!a-zA-Z]* | "")
            return -1
        ;;
        *)
            return 0
        ;;
    esac
}
aa_isdigit ()
{
    case $1 in
        *[!0-9]* | "")
            return -1
        ;;
        *)
            return 0
        ;;
    esac
}
aa_mkdir ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    local e=0;
    for f in ${1+"$@"};
    do
        set fnord `echo ":$f"|sed -e 's/^:\//%/' -e 's/^://' -e 's/\// /g' -e 's/^%/\//'`;
        shift;
        p=;
        for d in ${1+"$@"};
        do
            p="$p$d";
            case "$p" in
                -*)
                    p=./$p
                ;;
                ?:)
                    p="$p/";
                    continue
                ;;
            esac;
            [[ ! -d "$p" ]] && mkdir -v "$p" || e=$?;
            p="$p/";
        done;
    done;
    return $e
}
aa_motd ()
{
    [[ -r /etc/motd && `cat /etc/motd |wc -l` -gt 7 ]] && ( echo -e "\n\n${CC[2]}`head -n 7 /etc/motd | tail -n 6`${R}\n" ) && return;
    ahave figlet && figlet $HOSTNAME
}
aa_num_procs ()
{
    echo -n `command ps aux|wc -l`
}
aa_prompt ()
{
    AAPS[0]='\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[1;34m\u\e[0;34m@\e[1;34m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\e[1;30m\n\[${R}\]\$ ';
    AAPS[1]='\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[0;32m\u\e[1;32m@\e[0;32m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\e[1;30m\n\[${R}\]\$ ';
    AAPS[2]='\n\e[1;30m[\e[0;37m${SHLVL}\e[1;30m:\e[0;37m\j:\!\e[1;30m][\e[0;35m\u\e[1;35m@\e[0;35m\h\e[1;30m:\e[1;37m${SSHTTY/\/dev\/}\e[1;30m]\e[0;37m[\e[0;37m\w\e[0;37m]\e[1;30m\n\[${R}\]\$ ';
    : ${PLVL=0};
    [[ "${#AAPS[@]}" -lt "$PLVL" || "${#AAPS[@]}" -eq "$PLVL" ]] && PLVL=0;
    export PS1=${AAPS[$PLVL]} && (( PLVL++ )) && export PLVL;
    echo "PLVL=$PLVL"
}
aa_random_under ()
{
    echo $(( $RANDOM % ${1:-$RANDOM} + 1 ))
}
aa_safe_aliases ()
{
    local MYMVR=$(command type -t mymvmymv);
    [[ "$MYMVR" == "alias" ]] && echo "Turning Safe Aliases Off" && for fa in "chmod" "mkdir" "rm" "cp" "mv" "mymv";
    do
        unalias $fa;
    done && return;
    echo "Turning Safe Aliases On";
    alias chmod='command chmod -c';
    alias mkdir='command mkdir -pv';
    alias rm='command rm -v';
    alias cp='command cp -v';
    alias mv='command mv -v';
    alias mymv='echo'
}
aa_savehist ()
{
    history ${HISTCMD:-5000} | sed -e 's/^[ 0-9]*\(.*\)/\1/g' | tee -a $HISTFILEMASTER_C >>$HISTFILEMASTER;
    cat $HISTFILE | tee -a $HISTFILEMASTER_C >>$HISTFILEMASTER;
    history -w;
    echo
}
aa_show_all_colors ()
{
    for c in `seq 0 255`;
    do
        t=5;
        [[ $c -lt 108 ]] && t=0;
        for i in `seq $t 5`;
        do
            echo -e "\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d '[0-9]'`\e[0m";
        done;
    done;
    echo;
    echo Table for 16-color terminal escape sequences.;
    echo Replace ESC with \\033 in bash.;
    echo;
    echo "Background | Foreground colors";
    echo "---------------------------------------------------------------------";
    for ((bg=40; bg<=47; bg++))
    do
        for ((bold=0; bold<=1; bold++))
        do
            echo -en "\033[0m"" ESC[${bg}m   | ";
            for ((fg=30; fg<=37; fg++))
            do
                if [ $bold == "0" ]; then
                    echo -en "\033[${bg}m\033[${fg}m [${fg}m  ";
                else
                    echo -en "\033[${bg}m\033[1;${fg}m [1;${fg}m";
                fi;
            done;
            echo -e "\033[0m";
        done;
        echo "--------------------------------------------------------------------- ";
    done;
    echo;
    echo
}
aa_sleep ()
{
    echo -en "${CC[6]}${3:-.}";
    while `command ps -p $1 &>/dev/null`; do
        echo -n "${3:-.}";
        sleep ${3:-1};
    done;
    echo -e "$R$X" && sleep 1 && pd
}
aa_try_for_path ()
{
    local GP=$HOME/bin:$HOME/sbin;
    [[ -z "$PATH" || "$PATH" == "/bin:/usr/bin" ]] && export PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games";
    local P=$PATH:$HOME/libexec;
    [[ "$EUID" -eq 0 ]] && P="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11";
    P=${P}:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games;
    P=${P}:/bin:/etc:/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/bin/mh:/usr/libexec;
    P=${P}:/usr/X11R6/bin:/usr/libexec:/etc/X11:/etc/X11/xinit;
    [[ "$DREAMHOST" == "yes" ]] && ( P=${P}:/usr/local/dh/apache/template/bin:/usr/local/dh/apache2/template/bin;
    P=${P}:/usr/local/dh/apache2/template/build:/usr/local/dh/apache2/template/sbin;
    P=${P}:/usr/local/dh/bin:/usr/local/dh/java/bin:/usr/local/dh/java/jre/bin;
    P=${P}:/usr/lib/ruby/gems/1.8/bin:$HOME/.gem/ruby/1.8/bin );
    P=${P}:/usr/local/php5/bin;
    for p in ${P//:/ };
    do
        [[ -d "${p}" && -x "${p}" ]] && GP=${GP}:$p;
    done;
    export PATH=$( echo -en "${GP//:/\\n}" | sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; h; P' | tr "\n" : ).
}
aa_uniqhistory ()
{
    ( echo $HISTFILE;
    find $HISTFILEMASTER_DIR -type f 2>$N6 ) | xargs -iFFF cat FFF 2>$N6 | sed -e 's/^[ \t]*//;s/[ \t]*$//' -e '/[^ \t]\{1,\}/!d' | tr --squeeze ' ' | sort -u
}
aa_weather ()
{
    ahave lynx || return;
    local city weather=$HOME/.weather;
    echo -e "${CC[3]}";
    local res=`pkill -9 -f 15m &>$N6 || echo -n`;
    [[ -r $weather ]] && cat $weather && ( ( sleep 15m && command rm $HOME/.weather >&$N6 ) & );
    [[ ! -r $weather ]] && ( ( ( city=`lynx -dump http://api.hostip.info/get_html.php|sed -e '/^City/!d' -e 's/City: \([^,]*\), \(.*\)/\1+\2/g'` && lynx -dump "http://www.google.com/search?hl=en&q=weather+${city}" | sed -n '/Weather for/,/Search Res/p' | tr -d '\260' | sed -e '/iGoogle/d' -e 's/^[ \t]*//;s/[ \t]*$//' | sed '$d' | head -n 6 >$weather ) &  >&/dev/null ) );
    echo -e $R
}
aaup ()
{
    curl3 -0 -o ~/.bash_profile http://static.askapache.com/askapache-bash-profile.txt && dos2unix -dv ~/.bash_profile && ( rm -rf ~/{.bash_logout,.toprc,.lynxrc,.ncftp} || echo;
    echo ) && aa_savehist && exec bash -l
}
ahave ()
{
    unset -v ahave;
    command command type $1 >&$N6 && ahave="yes" || return 1
}
ahelp ()
{
    unset -v ahelp;
    [[ "$#" -gt "0" ]] && [[ "$1" == "-h" || "$1" == "--h" || "$1" == "--help" || "$1" == "-help" || "$1" == "-?" ]] && ahelp="yes"
}
arepeat ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    local i max=$1;
    shift;
    for ((i=1; i <= max ; i++))
    do
        eval "$@";
    done
}
ascript_title ()
{
    ahave tput && tput cup 0 3;
    clear;
    lin 0;
    echo -e "| ${CC[${1:-2}]}                ___       __    ___                 __                  ${CC[0]} |";
    echo -e "| ${CC[${1:-2}]}               / _ | ___ / /__ / _ | ___  ___ _____/ /  ___             ${CC[0]} |";
    echo -e "| ${CC[${2:-9}]}              / __ |(_-</  '_// __ |/ _ \/ _ \`/ __/ _ \/ -_)            ${CC[0]} |";
    echo -e "| ${CC[${2:-9}]}             /_/ |_/___/_/\_\/_/ |_/ .__/\_,_/\__/_//_/\__/             ${CC[0]} |";
    echo -e "| ${CC[${2:-9}]}                                  /_/                                   ${CC[0]} |";
    lin 1;
    lin 2 "${USER} ";
    lin 2 "${1:-$AAPN} ";
    lin 2 "Version ${2:-$AAPV} - Built: ${3:-$AAPT} ";
    lin 3
}
asetup_colors ()
{
    create_colors;
    ahave dircolors && eval "`dircolors -b`";
    export GREP_COLOR='
cho $R;
    [[ -r $HOME/.dircolors ]] && eval "`dircolors $HOME/.dircolors`" || dircolors --print-database >$HOME/.dircolors;
    local L="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31";
    L="${L}:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31";
    L="${L}:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35";
    L="${L}:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35";
    L="${L}:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.cpio=01;31";
    export LS_COLORS="${L}:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.htaccess=01;31:*.htpasswd=01;31:*.htpasswda1=01;31:*config.php=01;31:*wp-config.php=01;31:"
}
asetup_history ()
{
    export HISTFILE=${HISTFILE:-$HOME/.bash_history};
    export HISTCONTROL=${HISTCONTROL:-'ignoreboth'};
    export HISTSIZE=500;
    export HISTFILESIZE=500;
    export HISTIGNORE='clear:ls:ll:updatedb:top:h2:h1:h3:dir:cd ..:date:exit';
    unset HISTTIMEFORMAT;
    HISTFILEMASTER_DIR=$HOME/backups/.history;
    [[ ! -d "$HISTFILEMASTER_DIR/" ]] && mkdir -pv $HISTFILEMASTER_DIR;
    [[ ! -r ${HISTFILE} ]] && history -w $HISTFILE;
    HISTFILEMASTER=${HISTFILEMASTER_DIR}/combined.log;
    [[ ! -f $HISTFILEMASTER ]] && history -r && history -w $HISTFILEMASTER && history -c && echo "" >$HISTFILE;
    export HISTFILEMASTER;
    HISTFILEMASTER_C=$HISTFILEMASTER_DIR/combined-uniq.log;
    [[ ! -f "$HISTFILEMASTER_C" ]] && echo "" >$HISTFILEMASTER_C;
    export HISTFILEMASTER_C
}
askapache ()
{
    ascript_title;
    aa_motd;
    aa_weather;
    ahave who && pm "Users" && pm "Logged In" 3;
    command who -ar -pld && pm "Current Limits" 3;
    command ulimit -a;
    pm "Machine stats";
    ahave uptime && pm "uptime" 3 && command uptime;
    [[ -d /proc ]] && [[ -f /proc/meminfo ]] && cat /proc/meminfo;
    ahave who && pm "Users" 3 && command who;
    pm "Networking";
    ahave ip && pm "interfaces" 3 && ip -o addr | sed -e 's/ \{1,\}/\t/g';
    [[ -r /proc/net/sockstat ]] && pm "Sockets" 3 && head -n 2 /proc/net/sockstat;
    ahave ss && pm "Networking Stats" 3 && ss -s;
    ahave netstat && pm "Routing Information" 3 && netstat -r;
    pm "Disk";
    pm "Mounts" 3;
    command df -hai;
    ahave iostat && pm "I/O on Disks" && iostat -p ALL;
    pm "Processes";
    pm "process tree" 3;
    command ps -HAcl -F S -A f | uniq -w3;
    ahave procinfo && pm "procinfo" 3 && procinfo | head -n 13 | tail -n 11;
    pm "Functions";
    aa_functions;
    pm "Aliases";
    aa_aliases
}
clean_exit ()
{
    lin 0;
    lin 1;
    lin 2 "COMPLETED SUCCESSFULLY";
    lin 1;
    lin 3
}
cont ()
{
    local ans;
    echo -en "\n\n ${CC[15]}[ ${1:-Press any key to continue} ]$R\n\n";
    read -n 1 ans;
    aa_beep 1
}
create_colors ()
{
    for i in `seq 0 7`;
    do
        ii=$(($i+7));
        CC[$i]="\033[1;3${i}m";
        CC[$ii]="\033[0;3${i}m";
    done;
    CC[15]="\033[30;42m";
    export R=`tput sgr0`;
    export X='';
    export CC
}
curl1 ()
{
    command curl -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5' -e -L -H 'Accept: */*' "$@"
}
curl2 ()
{
    ( echo "\n\nurl_effective:\t\t%{url_effective}\ncontent_type:\t\t%{content_type}";
    echo "http_code:\t\t%{http_code}\t\tThe numerical code that was found in the last retrieved HTTPS page";
    echo "http_connect:\t\t%{http_connect}\t\tThe numerical code that was found in the last response from a proxy to a curl CONNECT request";
    echo "time_total:\t\t%{time_total}\t\tThe time will be displayed with millisecond resolution";
    echo "time_namelookup:\t%{time_namelookup}\t\tThe time, in seconds, it took from the start until the name resolving was completed";
    echo "time_connect:\t\t%{time_connect}\t\tThe time, in seconds, it took from the start until the connect to the remote host or proxy was completed";
    echo "time_pretransfer:\t%{time_pretransfer}\t\tThe time, in seconds, it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations to the particular protocols involved";
    echo "time_redirect:\t\t%{time_redirect}\t\tThe time, in seconds, it took for all redirection steps. time_redirect shows the complete execution time for multiple redirections";
    echo "time_starttransfer:\t%{time_starttransfer}\t\tThe time, in seconds, it took from the start until the first byte is just about to be transferred. This includes time_pretransfer and also the time the server needs to calculate the result";
    echo "size_download:\t\t%{size_download}\t\tThe total amount of bytes that were downloaded";
    echo "size_upload:\t\t%{size_upload}\t\tThe total amount of bytes that were uploaded";
    echo "size_header:\t\t%{size_header}\t\tThe total amount of bytes of the downloaded headers";
    echo "size_request:\t\t%{size_request}\t\tThe total amount of bytes that were sent in the HTTP request";
    echo "speed_download:\t\t%{speed_download}\tThe average download speed that curl measured for the complete download";
    echo "speed_upload:\t\t%{speed_upload}\t\tThe average upload speed that curl measured for the complete upload";
    echo "num_connects:\t\t%{num_connects}\t\tNumber of new connects made in the recent transfer" ) | curl "$@" -w '@-'
}
curl3 ()
{
    ahave curl && ( curl -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5' -e -L -b ~/.curl_cookie.$$ --cookie-jar ~/.curl_cookie.$$ -H 'Accept: */*' "$@" );
    ahave curl || ( command wget `sed -e 's/^-o /-O /g' -e 's/ -o / -O /g' -e 's/^--output /--output-file /g' -e 's/ --output / --output-file /g' <<< "$@"` )
}
cuttail ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    sed -n -e :a -e "1,${2:-10}!{P;N;D;};N;ba" $1
}
diffdirs ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    [[ "$#" -lt "2" ]] && echo "Usage: $FUNCNAME dir1 dir2" 1>&2 && return 2;
    ahave colordiff && colordiff -urp -w $1 $2
}
dirty_exit ()
{
    echo "See ya..";
    sleep 1;
    exit
}
do_sleep ()
{
    local E D;
    echo -en "${CC[6]}${3:-.}";
    while `command ps -p $RJ &>$N6`; do
        sleep ${2:-3};
        echo -en "${3:-.}";
    done;
    echo -e "${CC[0]}" && sleep 1 && pd
}
dos2unixx ()
{
    [[ $# -eq 0 ]] && exec tr -d '\015\032' || [[ ! -f "$1" ]] && echo "Not found: $1" && return;
    for f in "$@";
    do
        [[ ! -f "$f" ]] && continue;
        tr -d '\015\032' <"$f" >"$f.t" && cmp "$f" "$f.t" >$N6 && rm -f "$f.t" || ( touch -r "$f" "$f.t" && mv "$f" "$f.b" && mv "$f.t" "$f" && rm -f "$f.b" ) >&$N6;
    done
}
dump_ps_environment ()
{
    command ps aux | command grep ${USER:0:3} | command awk '{print $2}' | xargs -t -ipid /bin/sh -c 'test -r /proc/pid/environ && cat /proc/pid/environ | tr "\000" " "'
}
ex ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    until [[ -z "$1" ]]; do
        if [[ -f "$1" ]]; then
            pm "Extracting $1 ...";
            case $1 in
                *.tar.bz2)
                    tar xjf $1
                ;;
                *.tar.gz)
                    tar xzf $1
                ;;
                *.tar)
                    tar xf $1
                ;;
                *.tbz2)
                    tar xjf $1
                ;;
                *.tgz)
                    tar xzf $1
                ;;
                *.bz2)
                    bunzip2 $1
                ;;
                *.rar)
                    unrar x $1
                ;;
                *.gz)
                    gunzip $1
                ;;
                *.zip)
                    unzip $1
                ;;
                *.Z)
                    uncompress $1
                ;;
                *.7z)
                    7z x $1
                ;;
                *)
                    pm "Don't know how to extract '$1'"
                ;;
            esac;
        else
            pm "'$1' is not a valid file";
        fi;
        shift;
    done
}
exitall ()
{
    aa_savehist;
    pkill -9 -t "${SSH_TTY/\/dev\/}"
}
figtest ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME word" && return 2;
    for a in /usr/share/figlet/*.flf;
    do
        r=`basename ${a%%.flf}`;
        echo -e "${r}";
        figlet -t -f "$r" "$1";
    done
}
find_symlinks ()
{
    local O=$IFS;
    IFS='';
    for file in "$( find ${1:-`pwd`} -type l )";
    do
        echo "$file";
    done | sort
}
get_crypt_user ()
{
    ahave gpg || return;
    local U=`gpg --list-keys|sed -e '/^uid /!d' -e 's/^uid[ ]*\(.*\)/\1/g'` || unknown;
    [[ $# -ne 0 ]] && U=${U//\<*};
    echo -n $U
}
get_latest_revision ()
{
    local URL=${1:-http://svn.automattic.com/wordpress/trunk};
    svn info $URL | grep ^Rev | sed -e 's/Revision: \([0-9]*\)/\1/g'
}
grepc ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    command grep --color=always "$@"
}
grepp ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    command grep -i --color=always "$@" -r .
}
h2 ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME query" 1>&2 && return 2;
    command grep -h "$@" $HISTFILEMASTER_C
}
h2c ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME query" 1>&2 && return 2;
    command grep --color=always -h "$@" $HISTFILEMASTER_C
}
h2i ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME query" 1>&2 && return 2;
    command grep -h -i "$@" $HISTFILEMASTER_C
}
histfilemaster ()
{
    set -vx;
    history -a;
    [[ "`wc -l < $HISTFILE`" -gt "$HISTSIZE" ]] && ( cat $HISTFILE >>$HISTFILEMASTER;
    cat $HISTFILE >`date +$HISTFILEMASTER_DIR/%m-%d-%y.history`;
    cat $HISTFILE $HISTFILEMASTER_C | sort | uniq >$HISTFILEMASTER_DIR/hc.tmp;
    [[ "`wc -l < $HISTFILEMASTER_DIR/hc.tmp`" -gt "`wc -l < $HISTFILEMASTER_C`" ]] && mv $HISTFILEMASTER_DIR/hc.tmp $HISTFILEMASTER_C || cat $HISTFILE >>$HISTFILEMASTER_C;
    echo "" >$HISTFILE );
    set +vx
}
kill_jobs ()
{
    for i in `command jobs -p`;
    do
        kill -9 $i;
    done
}
l ()
{
    command ls -AhFp $AAC "$@"
}
la ()
{
    command du -kh * | awk '{print $2,$1}' | sort -n | tr ' ' "\t"
}
lin ()
{
    local L2 L1='__________________________________________________________________________';
    case ${1:-1} in
        0)
            echo -e "\n ${CC[0]}${L1}"
        ;;
        1)
            L2=`echo '                                                                          '`;
            echo -e "${CC[0]}|${CC[34]}${L2}${CC[0]}|"
        ;;
        2)
            echo -en "${CC[0]}|${CC[34]}";
            echo -en "${2:-1}" | sed -e :a -e 's/^.\{1,72\}$/ & /;ta' -e "s/\(.*\)/\1/";
            echo -e "${CC[0]} |"
        ;;
        3)
            echo -e "${CC[0]} ${L1} $R$X\n\n"
        ;;
    esac
}
ll ()
{
    command ls -lABls1c $AAC "$@"
}
locate1 ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    command locate "$@" | xargs -iFF stat -c %a\ %A\ \ A\ %x\ \ M\ %y\ \ C\ %z\ \ %N FF
}
make_ionice ()
{
    ahave ionice || return;
    local theclass thenicelevel thepid;
    thepid=${1:-$$};
    theclass=${2:-3};
    thenicelevel=${3:-7};
    pm "Making IONice class: $theclass, nicelevel: $thenicelevel, process: $thepid";
    pm 0 0;
    command ionice -c$theclass -n$thenicelevel -p $thepid;
    pd
}
make_nice ()
{
    ahave renice || return;
    local thenicelevel thepid;
    thepid=${1:-$$};
    thenicelevel=${2:-19};
    pm "Making process $thepid $thenicelevel nice.";
    pm 0 0;
    command renice $thenicelevel -p $theppid;
    pd
}
nh ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME <command>" 1>&2 && return 2;
    nohup "$@" >&$N6 & echo
}
nicest ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    local ncmd icmd='';
    ahave ionice && icmd='ionice -c3 -n7';
    ahave nice && ncmd='nice -n 19';
    $icmd $ncmd "$@";
    ahave ionice
}
nob ()
{
    [[ $# -eq 0 ]] && exec grep -v ^$ | sed -e '/[^ \t]\{1,\}/!d'
}
nobb ()
{
    [[ $# -eq 0 ]] && exec sed -e 's/^[ \t]*//;s/[ \t]*$//' -e '/[^ \t]\{1,\}/!d'
}
nobl ()
{
    [[ $# -eq 0 ]] && exec sed -e 's/^[ \t]*//'
}
nobt ()
{
    [[ $# -eq 0 ]] && exec sed -e 's/[ \t]*$//'
}
pd ()
{
    echo -e "\n ${CC[15]} ${1:-DONE} $R\n\n"
}
pm ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    local I=${1:-3};
    echo -en "$R\n";
    case ${2:-1} in
        0)
            echo -en "${CC[6]}-- $X$R"
        ;;
        1)
            echo -e "${CC[2]}>>> $X$I$R"
        ;;
        2)
            echo -en "${CC[4]} > $X$I$R"
        ;;
        3)
            echo -e "${CC[4]} :: $X$I$R"
        ;;
    esac
}
print_ascii_chart ()
{
    for i in `seq 0 256`;
    do
        echo -e "\\0$(( $i/64*100 + $i%64/8*10 + $i%8 ))";
    done
}
procinfo1 ()
{
    PI=($(strace -s1 procinfo -a 2>&1|sed -e '/^op/!d' -e '/pro/!d' -e '/= -1/d'|sed -e 's%o.*"/proc/\(.*\)".*% \1%g'));
    for i in ${PI[*]};
    do
        echo -e "\n---===[  /proc/$i  ]\n" && cat /proc/$i && echo -e "\n\n";
    done
}
ps ()
{
    [[ -z "$1" ]] && command ps -Hacl -F S -A f && return;
    command ps "$@"
}
pss ()
{
    local U PPS PL PX PXX UUS=($(command ps uax|awk '{print $1}'|command tail -n +2|sort|uniq));
    UL=$((${#UUS[@]} - 1));
    exec 6>&1;
    exec >~/proc.$$;
    ps aux | grep ${USER:0:3} | awk '{print $2}' | xargs -t -ipid cat /proc/pid/environ;
    for UX in $(seq 0 1 $UUS);
    do
        U=${UUS[$UX]};
        PPS=($(pgrep -u ${U}));
        PL=$((${#PPS[@]} - 1));
        for PX in $(seq 0 1 $PL);
        do
            PXX=${PPS[$PX]};
            echo -e "\n\n\n----- PROCESS ID: ${PXX} -----\n\n";
            cat /proc/${PXX}/cmdline 2>$N6 || echo;
            echo -e "\n\n";
            command tree -Csuflapi /proc/Q/${PXX};
        done;
    done;
    exec 1>&6 6>&-;
    cat ~/proc.$$ | more
}
psu ()
{
    command ps -Hcl -F S f -u ${1:-$USER}
}
pwd ()
{
    command pwd -LP "$@"
}
rmb ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME dirnametoremove" 1>&2 && return 2;
    ( ( command rm -rf "$@" >&$N6 ) & )
}
set_window_title ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    echo -n -e "\033]0;$*\007"
}
sleeper ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME <process id>" 1>&2 && return 2;
    echo -en "\n${2:-.}";
    while `command ps -p $1 &>$N6`; do
        echo -n "${2:-.}";
        sleep ${3:-1};
    done;
    echo
}
stat1 ()
{
    local D=${1:-$PWD/*};
    stat -c %a\ %A\ \ A\ %x\ \ M\ %y\ \ C\ %z\ \ %N ${D} | sed -e 's/ [0-9:]\{8\}\.[0-9]\{9\} -[0-9]\+//g' | tr -d "\`\'" | sort -r
}
stat2 ()
{
    local D=${1:-$PWD/*};
    stat -c %a\ %A\ \ A\ %x\ \ M\ %y\ \ C\ %z\ \ %N ${D} | sed -e 's/\.[0-9]\{9\} -[0-9]\+//g' | tr -d "\`\'" | sort -r
}
tard ()
{
    [[ "$#" -lt "2" ]] && echo "Usage: $FUNCNAME dest-file (auto adds tgz" 1>&2 && return 2;
    yn "Tarring $2 to $1" && ( ( command tar -zcpf "$1" "$2" >&$N6 ) & )
}
test_colors ()
{
    echo -e "$R";
    ahave tput && tput sgr0;
    for ((i=0; i<=${#CC[@]} - 1; i++))
    do
        echo -e "${CC[$i]}[$i]\n$R";
    done
}
uniq2 ()
{
    [[ -f "$1" ]] && sed = $1 | sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
}
uniqf ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    awk '!($0 in a) {a[$0];print}' "$1"
}
uptime1 ()
{
    command uptime | sed '/.*,  \([0-9]*\) users,  load average: \(.*\)/!d; s//[ \2, \1 users ]/;q'
}
vless ()
{
    [[ $# -eq 0 ]] && command vim --cmd 'let no_plugin_maps = 1' -c 'runtime! macros/less.vim' -;
    [[ $# -eq 0 ]] || command vim --cmd 'let no_plugin_maps = 1' -c 'runtime! macros/less.vim' "$@"
}
wcdir ()
{
    [[ "$#" -gt "0" && $1 == *-h* ]] && ahelp $1 && echo "Usage: $FUNCNAME" && return 2;
    find ${1:-.} -mindepth 1 -type d -print0 | xargs -0 -iFF sh -c 'echo `find "FF"/ -type f 2>/dev/null|wc -l;echo "FF"`' | sort -n | sed -e 's/^\([0-9]*\) \(.*\)$/ \1\t\2/g'
}
yes_no ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME Question" 1>&2 && return 2;
    local a YN=65;
    echo -en "${1:-Answer} [Y/n] ?";
    read -n 1 a;
    case $a in
        [yY])
            YN=0
        ;;
    esac;
    return $YN
}
yn ()
{
    [[ "$#" -lt "1" ]] && echo "Usage: $FUNCNAME " 1>&2 && return 2;
    local a YN=65;
    echo -en "\n ${CC[6]}@@ ${1:-Q} $R$X[y/N] ?$R";
    read -n 1 a;
    echo;
    case $a in
        [yY])
            echo -n "Y";
            YN=0
        ;;
        *)
            echo -n "N"
        ;;
    esac;
    return $YN
}

Custom bash profile for Advanced Shell Users


«
»

Leave your own comment

Reader Comments

  1. feigner ~

    First I want to thank askapache for sharing the fruit of his labor, then, not to complain, but rather query about an issue I’m having using the “bash_profile for Advanced Users.

    When I utilize this .bash_profile, I loose a feature that I really like in bash which is the use of special history completion using "!!", "!$", "!df", "!23", "!-3" and the like.

    I’ve looked at lots of bash documentation and even tried commenting out parts of you the profile to turn off history functions, but nothing works. Any Ideas?

  2. Productive Prompt « Get *NIXy! ~

    [...] Bonus: Some people seem to prefer two line prompts where the useful info is in the line above where you actually type your commands. I’m too much of a minimalist to be able to get behind this idea but if you’re interested, there’s a very robust and full-featured prompt that provides a lots of aliases, quotes, a banner, et cetera. It is available here. [...]

  3. Advanced WordPress wp-config.php Tweaks ~

    [...] For a better handle on the way I like to structure web site directories, see Optimize a Website for Speed, Security, and Easy Management but note it is a bit outdated compared to what I’m doing now. I don’t have the luxury of using only one type of server, or hosting provider anymore, so I have been working towards making things even more portable in order to move from host to host from server to server without issues i.e. my portable .bash_profile. [...]

  4. larsx2 ~

    Amazing! My bash_profile looked so skinny.. thanks for the share. (And truly it is bash that is the heart of evrything on askapache)

  5. Wojtek ~

    The script looks great but for some reason I’m getting:

    # ./askapache-bash-profile.sh
    -bash: ./askapache-bash-profile.sh: /bin/bash
    

    : bad interpreter: No such file or directory

    I do have bash :) and /bin/bash do exists. Any idea why is this happening?

    # dos2unix -dv askapache-bash-profile.sh
    

Go for it!


It's very simple - you read the protocol and write the code. -Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service

↑ TOP
Main