Kategorien

Druckansicht des Beitrags Druckansicht des Beitrags

Arbeitserleichterung durch .bashrc und .bash_aliases

Eine derjenigen Dateien, die man auf jeden Fall an seine persönlichen Bedürfnisse anpassen sollte, ist die .bashrc. Diese Datei ermöglicht es unter anderem, den Prompt der Bash-Shell zu setzen und eigene Funktionen sowie Aliasnamen für häufig benutzte Befehle festzulegen. Meine bashrc sieht aktuell so aus und enthält Code-Schnipsel von John Lawrence, geekosphere.org und Isaac Schlueter:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# ... and ignore same sucessive entries.
export HISTCONTROL=ignoreboth

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

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

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

function jobcount {
	jobs | wc -l | tr -d " "
}

# Color prompt
bash_color_prompt() {

	local BLACK="\[\e[0;30m\]"
	local BLUE="\[\e[0;34m\]"
	local GREEN="\[\e[0;32m\]"
	local CYAN="\[\e[0;36m\]"
	local RED="\[\e[0;31m\]"
	local PURPLE="\[\e[0;35m\]"
	local BROWN="\[\e[0;33m\]"
	local LIGHTGRAY="\[\e[0;37m\]"
	local DARKGRAY="\[\e[1;30m\]"
	local LIGHTBLUE="\[\e[1;34m\]"
	local LIGHTGREEN="\[\e[1;32m\]"
	local LIGHTCYAN="\[\e[1;36m\]"
	local LIGHTRED="\[\e[1;31m\]"
	local LIGHTPURPLE="\[\e[1;35m\]"
	local YELLOW="\[\e[1;33m\]"
	local WHITE="\[\e[1;37m\]"
	# No Color
	local NC="\[\e[0m\]"
	local cur_tty=$(tty | sed -e "s/.*tty\(.*\)/\1/")
	local loadavg='$(uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/" -e "s/ //g")'

	PS1="${LIGHTRED}\u${NC}@${LIGHTBLUE}\h${NC}-${GREEN}$(date +"%d.%m.%Y %H:%M:%S")${NC}:${YELLOW}\w\n${BROWN}[j:`jobcount`, t:$cur_tty, l:$loadavg]${WHITE}\n\\$ ${NC}"
}

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
	bash_color_prompt
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
    ;;
*)
    ;;
esac

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

#Sicherstellen, dass Sprache deutsch und Zeitzone für Deutschland passt
export LC_CTYPE=de_DE.UTF-8
export LC_ALL=""
export LANG=$LC_CTYPE
export LANGUAGE=$LANG
export TZ=Europe/Berlin

################## Start functions from http://www.perlmonks.org/?viewmode=public;node_id=359168 ###################################
# path-manipulating shell functions

function lspath {
    echo $PATH | tr ':' '\n'
}

function makepath {
    unset NEW_PATH
    while read PATHEL; do
        NEW_PATH="$NEW_PATH:$PATHEL"
    done
    echo ${NEW_PATH#:}
}

function uniqpath {
    PATH=`lspath | awk '{seen[$0]++; if (seen[$0]==1){print}}' | makepath`
}

function cleanpath {
    uniqpath
    PATH=`lspath | sed -e 's|\/$||' -ne '/./p' | makepath`
}

function addpath {
    PATH=$1:$PATH
    cleanpath
}

function appendpath {
    PATH=$PATH:$1
    cleanpath
}

function delpath {
    PATH=`lspath | grep -v "^$1\$" | makepath`
}

function editpath {
    TEMP=`mktemp "/tmp/${FUNCNAME}.XXXXXX"`
    lspath > $TEMP
    ${EDITOR:-vi} $TEMP
    PATH=`makepath < $TEMP`
    rm -f $TEMP
}
################## End functions from http://www.perlmonks.org/?viewmode=public;node_id=359168 ###################################

################## Begin functions from http://www.johnlawrence.net/code/?f=sfbashrc ###################################

#compare files using comm (requires perl) - http://www.shell-fu.org/lister.php?id=186
compare(){
  comm $1 $2 | perl -pe 's/^/1: /g;s/1: \t/2: /g;s/2: \t/A: /g;' | sort
}

# overwrite a file with zeroes - http://www.shell-fu.org/lister.php?id=94
zero() {
  case "$1" in
    "")     echo "Usage: zero "
            return -1;
  esac
  filesize=`wc -c  "$1" | awk '{print $1}'`
  dd if=/dev/zero of=$1 count=$filesize bs=1
}

# create a terminal calculator - http://www.shell-fu.org/lister.php?id=216
calc(){ echo "${1}"|bc -l; }

# copy and paste from the command line - http://www.shell-fu.org/lister.php?id=177
ccopy(){ cp $1 /tmp/ccopy.$1; }
alias cpaste="ls /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./%"

# bash function to decompress archives - http://www.shell-fu.org/lister.php?id=375
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
}
################## End functions from http://www.johnlawrence.net/code/?f=sfbashrc ###################################

################## Begin functions from http://foohack.com/tests/bash_extras/.extra.bashrc ###################################

# chooses the first argument that matches a file in the path.
choose_first () {
	for i in "$@"; do
		if ! [ -f "$i" ] && inpath "$i"; then
			i="`which $i`"
		fi
		if [ -f "$i" ]; then
			echo $i
			break
		fi
	done
}
inpath () {
	! [ $# -eq 1 ] && echo "usage: inpath <file>" && return 1
	f="`which $1 2>/dev/null`"
	[ -f "$f" ] && return 0
	return 1
}

# headless <command> [<key>]
# to reconnect, do: headless "" <key>
headless () {
	if [ "$2" == "" ]; then
		hash=`md5 -qs "$1"`
	else
		hash="$2"
	fi
	if [ "$1" != "" ]; then
		dtach -n /tmp/headless-$hash bash -l -c "$1"
	else
		dtach -A /tmp/headless-$hash bash -l
	fi
}

# function to do something in the background
back () {
	( $@ ) &
}
# do something very quietly.
quiet () {
	( $@ ) >/dev/null 2>/dev/null
}

# useful commands:
_set_editor () {
	edit_cmd="`choose_first $@`"
	if [ -f "$edit_cmd" ]; then
		if [ -f "${edit_cmd}_wait" ]; then
			export EDITOR="${edit_cmd}_wait"
		else
			export EDITOR="$edit_cmd"
		fi
	fi
	alias edit="$edit_cmd"
	alias sued="sudo $edit_cmd"
}
# my list of editors, by preference.
_set_editor mate vim vi pico ed

# shebang <file> <program> [<args>]
shebang () {
	sb="shebang"
	if [ $# -lt 2 ]; then
		echo "usage: $sb <file> <program> [<arg string>]"
		return 1
	elif ! [ -f "$1" ]; then
		echo "$sb: $1 is not a file."
		return 1
	fi
	if ! [ -w "$1" ]; then
		echo "$sb: $1 is not writable."
		return 1
	fi
	prog="$2"
	! [ -f "$prog" ] && prog="`which $prog 2>/dev/null`"
	if ! [ -x "$prog" ]; then
		echo "$sb: $2 is not executable, or not in path."
		return 1
	fi
	chmod ogu+x "$1"
	prog="#!$prog"
	[ "$3" != "" ] && prog="$prog $3"
	if ! [ "`head -n 1 \"$1\"`" == "$prog" ]; then
		contents="`cat \"$1\"`"
		newcontents=`cat <<ENDSHEBANG
$prog
$contents
ENDSHEBANG`
		echo -n "$newcontents" > $1
	fi
	return 0
}

rand () {
	# r=``
	# echo $r
	echo `php -r 'echo rand();'`
}

pickrand () {
	cnt=0
	if [ $# == 1 ]; then
		tst=$1
	else
		tst="-d"
	fi
	for i in *; do
		[ $tst "$i" ] && let 'cnt += 1'
	done
	r=`rand`
	p=0
	[ $cnt -eq 0 ] && return 1
	let 'p = r % cnt'
	# echo "[$cnt $r --- $p]"
	cnt=0
	for i in *; do
		# echo "[$cnt]"
		[ $tst "$i" ] && let 'cnt += 1' && [ $cnt -eq $p ] && echo "$i" && return
	done
}

# a friendlier delete on the command line
! [ -d ~/.Trash ] && mkdir ~/.Trash
chmod 700 ~/.Trash
alias emptytrash="find ~/.Trash -not -path ~/.Trash -exec rm -rf {} \; 2>/dev/null"
if ! inpath del; then
	if [ -d ~/.Trash ]; then
		del () {
			for i in "$@"; do
				mv "$i" ~/.Trash/
			done
		}
	else
		alias del=rm
	fi
fi

# find files in current dir by name (not in package dirs)
f () {
	find . -name "$1" -not -path "*/rhel.*.*.package/*" -not -path "*/CVS/*" -not -path "*/CVS" -not -path "*/rhel.*.*.package"
}

getip () {
	for each in $@; do
		echo $each
		echo "nslookup:"
		nslookup $each | grep Address: | grep -v '#' | egrep -o '([0-9]+\.){3}[0-9]+'
		echo "ping:"
		ping -c1 -t1 $each | egrep -o '([0-9]+\.){3}[0-9]+' | head -n1
	done
}

ips () {
  ip addr show | awk -F":" '($1>0){split($2,a," ");print a[1] "- "}($1 ~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/){split($1,a," "); print a[2]}' | tr '\n' '\t' | sed 's/\/[0-9]\+//g' | sed 's/\(\w\+\)-/\n\1/g' | sed '/^$/d' && echo
}

macs () {
  ip addr show | awk -F":" '($1>0){split($2,a," ");print a[1] "- "}($1 ~ /link/){split($0,a," "); print a[2]}' | tr '\n' '\t' | sed 's/\/[0-9]\+//g' | sed 's/\(\w\+\)-/\n\1/g' | sed '/^$/d' && echo
}

pg () {
	ps aux | grep $@ | grep -v grep
}
pid () {
	pg $@ | awk '{print $2}'
}

repeat () {
	if [ "$2" == "" ]; then
		delay=1
	else
		delay=$2
	fi
	while sleep $delay; do
		clear
		date +%s
		$1
	done
}

watch () {
	if [ "$2" == "" ]; then
		delay=1
	else
		delay="$2"
	fi
	i="";
	j="";
	while sleep $delay; do
		i=`$1`;
		if [ "$i" != "$j" ]; then
			date +%s
			echo $i
		fi
	done
}

# more persistent wget for fetching files to a specific filename.
fetch_to () {
	[ $# -ne 2 ] && echo "usage: fetch_to <url> <filename>" && return 1
	urltofetch=$1
	fname="$2"
	wget -O "$fname" $urltofetch
}

################## End functions from http://foohack.com/tests/bash_extras/.extra.bashrc ###################################

Durch Aufruf von

source ~/.bashrc

kann man ohne Neustart der Shell die Konfigurationsdatei laden. Die in der .bashrc referenzierte .bash_aliases sieht folgendermaßen aus:

# Alias definitions
# some more ls aliases
alias ll='ls -lF'
alias la='ls -aF'
alias lla='ls -laF'
alias l='ls -CF'
alias ld='ls -al -d * | egrep "^d"' # only subdirectories
alias lt='ls -alt | head -20' # recently changed files
alias ag="alias | $grep"

# convenience redefinitions
alias cd..='cd ..'
alias cd-='cd -'
alias dselect='dselect --expert'
alias fg-='fg -'
alias more="less -e"
alias lsdevs="sudo lsof | $grep /dev"
alias md='mkdir -p'
alias rd=rmdir
alias ..='cd ..'
alias ...='cd ../..'
#also remember popd
alias +='pushd .'
alias mvsafe="mv -i"

# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
    eval "`dircolors -b`"
    alias ls='ls --color=auto'
    alias dir='ls --color=auto --format=vertical'
    alias vdir='ls --color=auto --format=long'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# inventions
alias fgg=jobs
alias ghist='history|grep'
alias lf='find -type f|sort'
alias load='cat /proc/loadavg'
alias meminfo='cat /proc/meminfo'

# Kalendar
alias kal='clear;echo -n "Heutiges Datum: ";date;echo;cal -3m'

#make tree a little cooler looking.
alias tree="tree -CAFa -I 'CVS|rhel.*.*.package|.svn|.git' --dirsfirst"

alias processes="ps axMuc | egrep '^[a-zA-Z0-9]'"

# command-line perl prog
alias pie="perl -p -i -e "

################## Begin functions from http://www.johnlawrence.net/code/?f=sfbashrc ###################################

# directory tree - http://www.shell-fu.org/lister.php?id=209
alias dirf='find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"'

#calendar with today highlighted - http://www.shell-fu.org/lister.php?id=210
alias tcal='cal | sed "s/^/ /;s/$/ /;s/ $(date +%e) / $(date +%e | sed '\''s/./#/g'\'') /"'

# count files by type - http://www.shell-fu.org/lister.php?id=173
alias ftype='find ${*-.} -type f | xargs file | awk -F, '\''{print $1}'\'' | awk '\''{$1=NULL;print $0}'\'' | sort | uniq -c | sort -nr'

# convert permissions to octal - http://www.shell-fu.org/lister.php?id=205
alias lo='ls -l | sed -e 's/--x/1/g' -e 's/-w-/2/g' -e 's/-wx/3/g' -e 's/r--/4/g' -e 's/r-x/5/g' -e 's/rw-/6/g' -e 's/rwx/7/g' -e 's/---/0/g''

# portscan in one line - http://www.shell-fu.org/lister.php?id=295
portscan(){
  HOST="$1";for((port=1;port<=65535;++port));do echo -en "$port ";if echo -en "open $HOST $port\nlogout\quit" | telnet 2>/dev/null | grep 'Connected to' > /dev/null;then echo -en "\n\nport $port/tcp is open\n\n";fi;done
}

# print a random shell-fu tip - http://www.shell-fu.org/lister.php?id=192
alias shell-fu='links -dump "http://www.shell-fu.org/lister.php?random" | grep -A 100 -- ----'

# get an ordered list of subdirectory sizes - http://www.shell-fu.org/lister.php?id=275
alias dux='du -sk ./* | sort -n | awk '\''BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'\'''

# share current tree over the web - http://www.shell-fu.org/lister.php?id=54
alias webshare='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'

################## End functions from http://www.johnlawrence.net/code/?f=sfbashrc ###################################

1 comment to Arbeitserleichterung durch .bashrc und .bash_aliases

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.