bashrc example on Fedora (/etc/bashrc)

Moderator: analyzer

bashrc example on Fedora (/etc/bashrc)

Postby analyzer on 06 Mar 2008, 22:42

#!/bin/bash
# User: analyzer
# Filename: .bashrc
# Last Modified: 24/02/2008 20:59:21

# Current Format: [USER~@HOST] [dynamic section] [CURRENT DIRECTORY]$
# USER: (also sets the base color for the prompt)
# Red == Root(UID 0) Login shell (i.e. sudo bash)
# Light Red == Root(UID 0) Login shell (i.e. su -l or direct login)
# Yellow == Root(UID 0) priviledges in non-login shell (i.e. su)
# Brown == SU to user other than root(UID 0)
# Light Green == Normal user
# ~:
# Light Red == CCM_HOME environmental variable undefined.
# Gray == CCM_HOME environmental variable defined.
# @:
# Light Red == CCM_ADDR environmental variable undefined.
# Gray == CCM_ADDR environmental variable defined.
# HOST:
# Red == Insecure remote connection (unknown type)
# Yellow == Insecure remote connection (Telnet)
# Brown == Insecure remote connection (RSH)
# Cyan == Secure remote connection (i.e. SSH)
# Light Green == Local session
# DYNAMIC SECTION:
# (If count is zero for any of the following, it will not appear)
# [scr:#] ==== Number of detached screen sessions
# Yellow == 1-2
# Red == 3+
# [bg:#] ==== Number of backgrounded but still running jobs
# Yellow == 1-10
# Red == 11+
# [stp:#] ==== Number of stopped (backgrounded) jobs
# Yellow == 1-2
# Red == 3+
# CURRENT DIRECTORY: (truncated to 1/4 screen width)
# Red == Current user does not have write priviledges
# Light Green == Current user does have write priviledges
# NOTE:
# 1. Displays message on day change at midnight on the line above the
# prompt (Day changed to...).
# 2. Command is added to the history file each time you hit enter so its
# available to all shells.

# By default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi


# Configure Colors:
COLOR_WHITE='\033[1;37m'
COLOR_LIGHTGRAY='033[0;37m'
COLOR_GRAY='\033[1;30m'
COLOR_BLACK='\033[0;30m'
COLOR_RED='\033[0;31m'
COLOR_LIGHTRED='\033[1;31m'
COLOR_GREEN='\033[0;32m'
COLOR_LIGHTGREEN='\033[1;32m'
COLOR_BROWN='\033[0;33m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[0;34m'
COLOR_LIGHTBLUE='\033[1;34m'
COLOR_PURPLE='\033[0;35m'
COLOR_PINK='\033[1;35m'
COLOR_CYAN='\033[0;36m'
COLOR_LIGHTCYAN='\033[1;36m'
COLOR_DEFAULT='\033[0m'

# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

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

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

# 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 to set prompt_command to.
function promptcmd () {
# enable color support of ls and also add handy aliases
eval `dircolors -b`
alias ls='ls --color=always'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'

# some more ls aliases
alias ll='ls -AlF'
alias lt='ls -AlFt'
alias ltr='ls -AlFtr'
alias la='ls -AF'

# some more grep aliases
alias grep='grep --color=always'

# CLS Windows
alias cls='clear'

# Hex dump => 'hd /file | less'
alias hd='od -Ax -tx1z -v'

# My processus list
alias myps='ps -u "$USER" -o tty,user,pid,ppid,pcpu,pmem,args'

# Show whats going on !
alias rm='rm --verbose'
alias mv='mv --verbose'
alias cp='cp --verbose'

alias eterm='Eterm -Ox --scrollbar no --buttonbar no --font-fx none -f white Eterm -g 120x30+100+100 --shade 50 -c cyan'

# Calendar
alias cal='var=$(/usr/bin/cal -m); echo "${var/$(date +%-d)/$(echo -e "\033[1;31m$(date +%-d)\033[0m")}"'

# Wifi reloading
alias wifimonitor='ifconfig wlan0 down;iwconfig wlan0 mode monitor;ifconfig wlan0 up'
alias wifimanaged='ifconfig wlan0 down;iwconfig wlan0 mode managed;ifconfig wlan0 up'

# Update kernel + nvidia
alias yumbeta='yum --enable=updates-testing --enable=livna-testing update kernel kmod-nvidia'

# Dont put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
history -a

local SSH_FLAG=0
local TTY=$(tty | awk -F/dev/ '{print $2}')
if [[ ${TTY} ]]; then
local SESS_SRC=$(who | grep "$TTY " | awk '{print $6 }')
fi

# Titlebar
case ${TERM} in
xterm* )
local TITLEBAR='\[\033]0;[\u@\h] [\w] \007\]'
;;
* )
local TITLEBAR=''
;;
esac
PS1="${TITLEBAR}"

# Test for day change.
if [ -z $DAY ] ; then
export DAY=$(date +%A)
else
local today=$(date +%A)
if [ "${DAY}" != "${today}" ]; then
PS1="${PS1}\n\[${COLOR_LIGHTGREEN}\]Day changed to $(date '+%A, %d %B %Y').\n"
export DAY=$today
fi
fi

# Black Bracket
PS1="${PS1}\[${COLOR_GRAY}\]["

# User
if [ ${UID} -eq 0 ] ; then
if [ "${USER}" == "${LOGNAME}" ]; then
if [[ ${SUDO_USER} ]]; then
PS1="${PS1}\[${COLOR_RED}\]\u"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]\u"
fi
else
PS1="${PS1}\[${COLOR_YELLOW}\]\u"
fi
else
if [ ${USER} == ${LOGNAME} ]; then
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]\u"
else
PS1="${PS1}\[${COLOR_BROWN}\]\u"
fi
fi

# ETH0 UP / DOWN / PROMISCUOUS
if [ `ifconfig eth0 | grep -c "UP"` != "0" ] ; then
if [ `ifconfig eth0 | grep -c "PROMISC"` != "0" ] ; then
PS1="${PS1}\[${COLOR_YELLOW}\]@"
else
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]@"
fi
else
PS1="${PS1}\[${COLOR_GRAY}\]@"
fi

# WLAN0 UP / DOWN / MONITOR
if [ `ifconfig wlan0 | grep -c "UP"` != "0" ] ; then
if [ `iwconfig wlan0 | grep -c -e "Monitor"` != "0" ] ; then
PS1="${PS1}\[${COLOR_YELLOW}\]~"
else
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]~"
fi
else
PS1="${PS1}\[${COLOR_GRAY}\]~"
fi

# Host
if [[ ${SSH_CLIENT} ]] || [[ ${SSH2_CLIENT} ]]; then
SSH_FLAG=1
fi
if [ ${SSH_FLAG} -eq 1 ]; then
PS1="${PS1}\[${COLOR_CYAN}\]\h"
elif [[ -n ${SESS_SRC} ]]; then
if [ "${SESS_SRC}" == "(:0.0)" ]; then
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]\h"
else
local parent_process=`cat /proc/${PPID}/cmdline`
if [[ "$parent_process" == "in.rlogind*" ]]; then
PS1="${PS1}\[${COLOR_BROWN}\]\h"
elif [[ "$parent_process" == "in.telnetd*" ]]; then
PS1="${PS1}\[${COLOR_YELLOW}\]\h"
else
PS1="${PS1}\[${COLOR_RED}\]\h"
fi
fi
elif [[ "${SESS_SRC}" == "" ]]; then
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]\h"
else
PS1="${PS1}\[${COLOR_LIGHTRED}\]\h"
fi

# Black Bracket
PS1="${PS1}\[${COLOR_GRAY}\]] "

# Detached Screen Sessions
local DTCHSCRN=$(screen -ls | grep -c Detach )
if [ ${DTCHSCRN} -gt 2 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_RED}\]scr\[${COLOR_GRAY}\]:\[${COLOR_RED}\]${DTCHSCRN}\[${COLOR_GRAY}\]] "
elif [ ${DTCHSCRN} -gt 0 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_YELLOW}\]scr\[${COLOR_GRAY}\]:\[${COLOR_YELLOW}\]${DTCHSCRN}\[${COLOR_GRAY}\]] "
fi

# Backgrounded running jobs
local BKGJBS=$(jobs -r | wc -l )
if [ ${BKGJBS} -gt 2 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_RED}\]bg\[${COLOR_GRAY}\]:\[${COLOR_RED}\]${BKGJBS}\[${COLOR_GRAY}\]] "
elif [ ${BKGJBS} -gt 0 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_YELLOW}\]bg\[${COLOR_GRAY}\]:\[${COLOR_YELLOW}\]${BKGJBS}\[${COLOR_GRAY}\]] "
fi

# Stopped Jobs
local STPJBS=$(jobs -s | wc -l )
if [ ${STPJBS} -gt 2 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_RED}\]stp\[${COLOR_GRAY}\]:\[${COLOR_RED}\]${STPJBS}\[${COLOR_GRAY}\]] "
elif [ ${STPJBS} -gt 0 ]; then
PS1="${PS1}\[${COLOR_GRAY}\][\[${COLOR_YELLOW}\]stp\[${COLOR_GRAY}\]:\[${COLOR_YELLOW}\]${STPJBS}\[${COLOR_GRAY}\]] "
fi


# Black Bracket
PS1="${PS1}\[${COLOR_GRAY}\]["

# Working directory
if [ -w "${PWD}" ]; then
PS1="${PS1}\[${COLOR_LIGHTGREEN}\]$(prompt_workingdir)"
else
PS1="${PS1}\[${COLOR_RED}\]$(prompt_workingdir)"
fi

# Black Bracket + End
PS1="${PS1}\[${COLOR_GRAY}\]]\[${COLOR_WHITE}\]"

if [ ${UID} -eq 0 ] ; then
PS1="${PS1}#\[${COLOR_DEFAULT}\] "
else
PS1="${PS1}\$\[${COLOR_DEFAULT}\] "
fi
}

# Trim working dir to 1/4 the screen width
function prompt_workingdir () {
# local pwdmaxlen=$(($COLUMNS/4))
# local trunc_symbol="..."
# if [[ $PWD == $HOME* ]]; then
# newPWD="~${PWD#$HOME}"
# else
# newPWD=${PWD}
# fi
# if [ ${#newPWD} -gt $pwdmaxlen ]; then
# local pwdoffset=$(( ${#newPWD} - $pwdmaxlen + 3 ))
# newPWD="${trunc_symbol}${newPWD:$pwdoffset:$pwdmaxlen}"
# fi
# echo $newPWD
echo "\w"
}

function startcm() {
export CCM_ADDR=`ccm start -d $CCM_DB/$1 -h UHZCM001 -m -nogui -q -r developer`
}

function quitcm() {
ccm stop
export CCM_ADDR=
}

function search() {
cat ~/.bash_history | sort -u | grep -e "$*"
}

# Determine what prompt to display:
# 1. Display simple custom prompt for shell sessions started
# by script.
# 2. Display "bland" prompt for shell sessions within emacs or
# xemacs.
# 3 Display promptcmd for all other cases.

function load_prompt () {
# Get PIDs
local parent_process=$(cat /proc/$PPID/cmdline | cut -d \. -f 1)
local my_process=$(cat /proc/$$/cmdline | cut -d \. -f 1)

if [[ $parent_process == script* ]]; then
PROMPT_COMMAND=""
PS1="\t - \# - \u@\H { \w }"
elif [[ $parent_process == emacs* || $parent_process == xemacs* ]]; then
PROMPT_COMMAND=""
PS1="\u@\h { \w }"
else
export DAY=$(date +%A)
#PROMPT_COMMAND="\[\033[1;30m\][\[\033[1;34m\]\u\[\033[1;30m\]@\[\033[0;35m\]\h\[\033[1;30m\]] \[\033[0;37m\]\W \[\033[1;30m\]\$\[\033[0m\] "
PROMPT_COMMAND=promptcmd
fi
export PS1 PROMPT_COMMAND
}

if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}

for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
. $i
fi
done
unset i
unset pathmunge
fi
# vim:ts=4:sw=4

load_prompt
analyzer
Administrateur
Administrateur
 
Posts: 27
Joined: 22 Mar 2005, 19:37

Return to Fedora

Who is online

Users browsing this forum: No registered users and 1 guest

cron