Skip to content

Instantly share code, notes, and snippets.

@mbround18
Last active October 14, 2022 04:02
Show Gist options
  • Save mbround18/650d59476b86fbe885e66af953099006 to your computer and use it in GitHub Desktop.
Save mbround18/650d59476b86fbe885e66af953099006 to your computer and use it in GitHub Desktop.
Personal bashrc
#--------------------------------------------------------------------------------------------
# if found on gist use `git clone https://gist.github.com/650d59476b86fbe885e66af953099006.git .`
# this is a modified version of Emmanuel Rouat [no-email] bashrc how to which can be found at
# `http://tldp.org/LDP/abs/html/sample-bashrc.html`
#--------------------------------------------------------------------------------------------
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
#-------------------------------------------------------------
# Source global definitions (if any)
#-------------------------------------------------------------
# For Arch
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc # --> Read /etc/bash.bashrc, if present.
fi
# From Example
if [ -f /etc/bashrc ]; then
. /etc/bashrc # --> Read /etc/bashrc, if present.
fi
if [ -f $HOME/.bashrc_ext ]; then
. $HOME/.bashrc_ext # --> Designed to read a file that is not uploaded anywhere due to sensitive data
fi
if [ -f /usr/share/bash-completion/bash_completion ]; then
[[ $PS1 && -f /usr/share/bash-completion/bash_completion ]] && \
. /usr/share/bash-completion/bash_completion
fi
#--------------------------------------------------------------edit
# Automatic setting of $DISPLAY (if not set already).
# This works for me - your mileage may vary. . . .
# The problem is that different types of terminals give
#+ different answers to 'who am i' (rxvt in particular can be
#+ troublesome) - however this code seems to work in a majority
#+ of cases.
#--------------------------------------------------------------
get_xserver()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
# Ane-Pieter Wieringa suggests the following alternative:
# I_AM=$(who am i)
# SERVER=${I_AM#*(}
# SERVER=${SERVER%*)}
XSERVER=${XSERVER%%:*}
;;
aterm | rxvt)
# Find some code that works here. ...
;;
esac
}
if [ -z ${DISPLAY:=""} ]; then
get_xserver
if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) ||
${XSERVER} == "unix" ]]; then
DISPLAY=":0.0" # Display on local host.
else
DISPLAY=${XSERVER}:0.0 # Display on remote host.
fi
fi
export DISPLAY
#-------------------------------------------------------------
# Some settings
#-------------------------------------------------------------
#set -o nounset # These two options are useful for debugging.
#set -o xtrace
alias debug="set -o nounset; set -o xtrace"
ulimit -S -c 0 # Don't want coredumps.
set -o notify
set -o noclobber
set -o ignoreeof
# Enable options:
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s cmdhist
shopt -s histappend histreedit histverify
shopt -s extglob # Necessary for programmable completion.
# Disable options:
shopt -u mailwarn
unset MAILCHECK # Don't want my shell to warn me of incoming mail.
#-------------------------------------------------------------
# Greeting, motd etc. ...
#-------------------------------------------------------------
# Color definitions (taken from Color Bash Prompt HowTo).
# Some colors might look different of some terminals.
# For example, I see 'Bold Red' as 'orange' on my screen,
# hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt.
# Normal Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
NC="\e[m" # Color Reset
ALERT=${BWhite}${On_Red} # Bold White on red background
#-----MOVED TO BOTTOM OF FILE DUE TO ORGANIZATIONL STUFF----------
# echo -e "${BCyan}This is BASH ${BRed}${BASH_VERSION%.*}${BCyan}\
# - DISPLAY on ${BRed}$DISPLAY${NC}\n"
# date
#-----------------------------------------------------------------
_exit() # to run upon exit of shell.
{
echo -e "${BRed}Hasta la vista, baby${NC}"
}
trap _exit EXIT
#-------------------------------------------------------------
# Shell Prompt - for many examples, see:
# http://www.debian-administration.org/articles/205
# http://www.askapache.com/linux/bash-power-prompt.html
# http://tldp.org/HOWTO/Bash-Prompt-HOWTO
# https://github.com/nojhan/liquidprompt
#-------------------------------------------------------------
# Current Format: [TIME USER@HOST PWD] >
# TIME:
# Green == machine load is low
# Orange == machine load is medium
# Red == machine load is high
# ALERT == machine load is very high
# USER:
# Cyan == normal user
# Orange == SU to user
# Red == root
# HOST:
# Cyan == local session
# Green == secured remote connection (via ssh)
# Red == unsecured remote connection
# PWD:
# Green == more than 10% free disk space
# Orange == less than 10% free disk space
# ALERT == less than 5% free disk space
# Red == current user does not have write privileges
# Cyan == current filesystem is size zero (like /proc)
# >:
# White == no background or suspended jobs in this shell
# Cyan == at least one background job in this shell
# Orange == at least one suspended job in this shell
#
# Command is added to the history file each time you hit enter,
# so it's available to all shells (using 'history -a').
# Test connection type:
if [ -n "${SSH_CONNECTION}" ]; then
CNX=${Green} # Connected on remote machine, via ssh (good).
elif [[ "${DISPLAY%%:0*}" != "" ]]; then
CNX=${ALERT} # Connected on remote machine, not via ssh (bad).
else
CNX=${BCyan} # Connected on local machine.
fi
# Test user type:
if [[ ${USER} == "root" ]]; then
SU=${Red} # User is root.
# elif [[ ${USER} != $(logname) ]]; then
# SU=${BRed} # User is not login user.
else
SU=${BCyan} # User is normal (well ... most of us are).
fi
NCPU=$(grep -c 'processor' /proc/cpuinfo) # Number of CPUs
SLOAD=$(( 100*${NCPU} )) # Small load
MLOAD=$(( 200*${NCPU} )) # Medium load
XLOAD=$(( 400*${NCPU} )) # Xlarge load
# Returns system load as percentage, i.e., '40' rather than '0.40)'.
load()
{
local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
# System load of the current host.
echo $((10#$SYSLOAD)) # Convert to decimal.
}
# Returns a color indicating system load.
load_color()
{
local SYSLOAD=$(load)
if [ ${SYSLOAD} -gt ${XLOAD} ]; then
echo -en ${ALERT}
elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
echo -en ${Red}
elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
echo -en ${BRed}
else
echo -en ${Green}
fi
}
# Returns a color according to free disk space in $PWD.
disk_color()
{
if [ ! -w "${PWD}" ] ; then
echo -en ${Red}
# No 'write' privilege in the current directory.
elif [ -s "${PWD}" ] ; then
local used=$(command df -P "$PWD" |
awk 'END {print $5} {sub(/%/,"")}')
if [ ${used} -gt 95 ]; then
echo -en ${ALERT} # Disk almost full (>95%).
elif [ ${used} -gt 90 ]; then
echo -en ${BRed} # Free disk space almost gone.
else
echo -en ${Green} # Free disk space is ok.
fi
else
echo -en ${Cyan}
# Current directory is size '0' (like /proc, /sys etc).
fi
}
# Returns a color according to running/suspended jobs.
job_color()
{
if [ $(jobs -s | wc -l) -gt "0" ]; then
echo -en ${BRed}
elif [ $(jobs -r | wc -l) -gt "0" ] ; then
echo -en ${BCyan}
fi
}
# Adds some text in the terminal frame (if applicable).
# Now we construct the prompt.
PROMPT_COMMAND="history -a"
case ${TERM} in
*term | rxvt | linux)
PS1="\[\$(load_color)\][\A\[${NC}\] "
# Time of day (with load info):
PS1="\[\$(load_color)\][\A\[${NC}\] "
# User@Host (with connection type info):
PS1=${PS1}"\[${SU}\]\u\[${Green}\]@\[${Purple}\]\h\[${NC}\] "
# PWD (with 'disk space' info):
PS1=${PS1}"\[\$(disk_color)\]\W]\[${NC}\] "
# Prompt (with 'job' info):
PS1=${PS1}"\[\$(job_color)\]>\[${NC}\] "
# Set title of current xterm:
PS1=${PS1}"\[\e]0;[\u@\h] \w\a\]"
;;
*)
PS1="(\A \u@\h \W) > " # --> PS1="(\A \u@\h \w) > "
# --> Shows full pathname of current dir.
;;
esac
export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
export HISTIGNORE="&:bg:fg:ll:h"
export HISTTIMEFORMAT="$(echo -e ${BCyan})[%d/%m %H:%M:%S]$(echo -e ${NC}) "
export HISTCONTROL=ignoredups
export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts
#============================================================
#
# ALIASES AND FUNCTIONS
#
# Arguably, some functions defined here are quite big.
# If you want to make this file smaller, these functions can
#+ be converted into scripts and removed from here.
#
#============================================================
#-------------------
# Personnal Aliases
#-------------------
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# -> Prevents accidentally clobbering files.
alias mkdir='mkdir -p'
alias h='history'
alias j='jobs -l'
alias which='type -a'
alias ..='cd ..'
# Pretty-print of some PATH variables:
alias path='echo -e ${PATH//:/\\n}'
alias libpath='echo -e ${LD_LIBRARY_PATH//:/\\n}'
alias du='du -kh' # Makes a more readable output.
alias df='df -kTh'
alias youtube-cli='mpsyt'
#-------------------
# Personnal Exports
#-------------------
export EDITOR=nano
export BASHRC="$HOME/.bashrc"
export PATH="$PATH:$HOME/.gem/ruby/2.3.0/bin"
export LANG=en_US.UTF-8
#-----------------------------------
# Personal Additions
#-----------------------------------
if [ -f "/etc/profile.d/vte.sh" ]; then
. /etc/profile.d/vte.sh
fi
config-git()
{
if [ -z "$1" ]; then
echo "No name supplied!! Please rerun this command with 'config-git [name you wih to use] [email you wish to use]'"
elif [ -z "$2" ]; then
echo "No email supplied!! Please rerun this command with 'config-git [name you wih to use] [email you wish to use]'"
else
git config --global user.name "$1"
git config --global user.email "$2"
git config --global core.editor $EDITOR
fi
}
export -f config-git
update-bashrc()
{
echo -e "grabbing updated version"
git clone https://gist.github.com/650d59476b86fbe885e66af953099006.git ~/pbashrctmp_650d59476b86fbe885e66af953099006 >/dev/null 2>&1
mv -f ~/pbashrctmp_650d59476b86fbe885e66af953099006/.bashrc ~/ >/dev/null 2>&1
echo -e "sourcing new version"
source ~/.bashrc
rm -rf ~/pbashrctmp_650d59476b86fbe885e66af953099006 >/dev/null 2>&1
}
export -f update-bashrc
update-pip-packages()
{
if hash pip 2>/dev/null; then
sudo -H pip install --upgrade pip
sudo -H pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 sudo -H pip install -U
fi
if hash pip3 2>/dev/null; then
sudo -H pip3 install --upgrade pip
sudo -H pip3 freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 sudo -H pip3 install -U
fi
}
export -f update-pip-packages
update-everything()
{
echo "Beginning update..."
update-bashrc >/dev/null 2>&1
# OS Packages
if hash yaourt 2>/dev/null; then
yaourt -Syu --aur
elif hash pacman 2>/dev/null; then
sudo pacman -Syu
fi
printf "."
if hash aptitude 2>/dev/null; then
sudo aptitude update && sudo aptitude upgrade
elif hash apt-get 2>/dev/null; then
sudo apt-get update && sudo apt-get upgrade
fi
printf "."
if hash gem 2>/dev/null; then
gem update >/dev/null 2>&1
fi
printf "."
if hash pip 2>/dev/null; then
echo -e " updating all pip packages"
update-pip-packages >/dev/null 2>&1
echo -e " update for pip packages complete"
fi
printf "."
printf "update complete\n"
}
export -f update-everything
web2pdf() # This function needs two arguments, one being url/file the other bing a the output-name.pdfs
{
if hash wkhtmltopdf 2>/dev/null; then
NOW=$(date +"%m_%d_%Y")
if curl --output /dev/null --silent --fail -r 0-0 "$1" && [ ! -z "$2" ]; then
wkhtmltopdf "$1" "$NOW-$2"
elif [ -f "$1" ] && [ ! -z "$2" ]; then
case "$1" in
*.html)
wkhtmltopdf "$1" "$2"
;;
*)
mkdir TMP
mv "$1" TMP
(cd TMP && extract "$1")
mv -f TMP/*.html TMP/index.html
wkhtmltopdf TMP/index.html "$NOW-$2"
rm -rf TMP "$1"
;;
esac
elif [[ -f "$1" ]]; then
echo "Not enough arguments please use web2pdf file/url output-name.pdf"
else
echo "'$1' is not a valid file or URL!"
fi
else
echo "I require wkhtmltopdf to work, see http://wkhtmltopdf.org/"
fi
}
export -f web2pdf
if hash md5sum 2>/dev/null; then
md5sumdir() {
if [ -d "$1" ]; then
find "$1" -type f -print0 | xargs -0 md5sum >> "$2"
else
echo -e "Error: no directory found matching : $1 "
fi
}
export -f md5sumdir
fi
diff2files()
{
EXT1=$(echo "$1" | awk -F . '{print $NF}') || EXT1=""
EXT2=$(echo "$2" | awk -F . '{print $NF}') || EXT2=""
if [ "$EXT1" == "$EXT2" ] ; then
case "$1" in
*.csv | *.txt | *.xml | *.html | *.json | *.css | *.rb)
NOW=$(date +"%m_%d_%Y_%H_%M")
grep -xvFf "$1" "$2" > "diff-$NOW.$EXT1"
;;
*)
echo "The files you have selected are not a supported extension!!" ;;
esac
else
echo "These are not the same extension!!"
fi
}
export -f diff2files
nullify() {
"$@" >/dev/null 2>&1 &
}
export -f nullify
if hash yaourt 2>/dev/null; then
remove-dependencies()
{
yaourt -R $(yaourt -Qdtq)
}
export -f remove-dependencies
fi
update-packages()
{
if hash yaourt 2>/dev/null; then
yaourt -Syua --aur
elif hash apt-get 2>/dev/null; then
sudo apt-get update
else
if hash pacman 2>/dev/null; then
sudo pacman -Syu
fi
fi
}
export -f update-packages
reload-usb()
{
sudo modprobe -r usbhid; sudo modprobe usbhid
}
export -f reload-usb
get-public-ip()
{
echo -e $(wget http://ipinfo.io/ip -qO -)
}
export -f get-public-ip
get-local-ip()
{
if hash ip 2>/dev/null; then
echo -e $(ip a | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
elif hash ifconfig 2>/dev/null; then
echo -e $(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
fi
}
export -f get-local-ip
enable-adblock()
{
declare -a arr=(
"http://hosts-file.net/ad_servers.txt"
"http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
"http://winhelp2002.mvps.org/hosts.txt"
"https://adaway.org/hosts.txt"
)
touch /tmp/test_merge_adblock.txt
echo -e "# this file was generated at $(date) using the following host files:" > /tmp/hosts_complete
## now loop through the above array
for i in "${arr[@]}"
do
echo -e "downloading source host: $i"
TEMP_TXT_NAME=$(printf "$i" | sha256sum | sed 's/\ //g' | sed 's/\-//g')
(curl "$i" > /tmp/"$TEMP_TXT_NAME"_adblock.txt) >/dev/null 2>&1
(sed -i 's/\r//' /tmp/"$TEMP_TXT_NAME"_adblock.txt)
cat /tmp/"$TEMP_TXT_NAME"_adblock.txt >> /tmp/test_merge_adblock.txt
echo -e "# $i" >> /tmp/hosts_complete
done
echo -e "all hosts downloaded"
(sed '/^#/ d' /tmp/test_merge_adblock.txt > /tmp/test_tmp_merge_adblock.txt) >/dev/null 2>&1
mv -f /tmp/test_tmp_merge_adblock.txt /tmp/test_merge_adblock.txt
awk '!seen[$0]++' /tmp/test_merge_adblock.txt >> /tmp/hosts_complete
sudo mv /tmp/hosts_complete /etc/hosts
sudo chown root:root /etc/hosts
rm -rf /tmp/*_adblock.txt
rm -rf /tmp/hosts_complete
echo -e "all hosts merged"
}
export -f enable-adblock
disable-adblock()
{
sudo rm -rf /etc/hosts
echo -e "#
# /etc/hosts: static lookup table for host names
#
#<ip-address> <hostname.domain.org> <hostname>
127.0.0.1 localhost.localdomain localhost
::1 localhost.localdomain localhost
# End of file" > /tmp/hosts_complete
sudo mv /tmp/hosts_complete /etc/hosts
sudo chown root:root /etc/hosts
rm -rf /tmp/hosts_complete
}
export -f disable-adblock
if hash rankmirrors 2>/dev/null;then
get-fastest-mirrorlist() {
temp_file_name=mirrorlist_"$(date -d "today" +"%Y%m%d")".backup
echo -e "Backing up current mirrorlist to $temp_file_name"
sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/"$temp_file_name"
echo -e "Removing commented out mirrors"
sudo sed '/^#\S/ s|#||' -i /etc/pacman.d/"$temp_file_name"
echo -e "Removing old mirror list"
sudo rm -rf /etc/pacman.d/mirrorlist
echo -e "Running rankmirrors against the backup to find the fastest 6 mirrors"
sudo su root -c 'rankmirrors -n 6 --verbose /etc/pacman.d/mirrorlist_"$(date -d "today" +"%Y%m%d")".backup >> /etc/pacman.d/mirrorlist'
sudo pacman -Syy
echo -e "Process Completed!"
}
export -f get-fastest-mirrorlist
fi
function screen() {
/usr/bin/script -q -c "/usr/bin/screen ${*}" /dev/null
}
export -f screen
function chgusrname() {
echo "changing username $1 to username $2"
sudo groupadd "$2"
sudo usermod -d /home/"$2" -m -g "$2" -l "$2" "$1"
sleep 1
echo "completed! :) say Hi to $2 who was previously $1"
}
export -f chgusrname
allgroups() {
cut -d: -f1 /etc/group | sort -n -k5
}
export -f allgroups
alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)'
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'
docker-clean-house() {
docker stop $(docker ps -a -q)
docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -a -q)
}
export -f docker-clean-house
if hash nginx 2>/dev/null; then
nginx-test-config() {
sudo rm -rf /etc/nginx/sites-enabled/* && sudo cp /etc/nginx/sites-available/* /etc/nginx/sites-enabled/ && sudo nginx -t
}
export -f nginx-test-config
fi
get-system-unique-id() {
echo $(sudo dmidecode -t 4 | grep ID | sed 's/.*ID://;s/ //g')
}
export -f get-system-unique-id
generate-unique-id() {
echo $(cat /proc/sys/kernel/random/uuid)
}
feast-on-the-blood-of-the-innocent()
{
echo -e "meow"
}
export -f feast-on-the-blood-of-the-innocent
# just some minor astetics for my own personal convienence.
alias 'sbashrc'='source $BASHRC'
alias 'ebashrc'='$EDITOR $BASHRC'
alias 'gebashrc'='$GEDITOR $BASHRC'
if hash fcrontab 2>/dev/null; then alias 'crontab'='fcrontab'; fi
# point to terminator
# alias terminal=terminator
# alias xterm=terminator
# alias uxterm=terminator
# PERSONAL RUBY MINE LOCATION
alias rubymine='sh $HOME/development/RubyMine/bin/rubymine.sh'
if hash thefuck 2>/dev/null; then
TF_ALIAS=fuck alias fuck='PYTHONIOENCODING=utf-8 eval $(thefuck $(fc -ln -1)); history -r'
fi
if [ -d "$HOME/.rvm" ]; then
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
fi
if [ -d "$HOME/.rbenv" ]; then
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
fi
if hash xclip 2>/dev/null; then
alias setclip='xclip -selection c'
alias getclip="xclip -selection clipboard -o; echo -ne '\n';"
fi
alias dd='dd status=progress'
#-------------------------------------------------------------
# The 'ls' family (this assumes you use a recent GNU ls).
#-------------------------------------------------------------
# Add colors for filetype and human-readable sizes by default on 'ls':
alias ls='ls -h --color'
alias lx='ls -lXB' # Sort by extension.
alias lk='ls -lSr' # Sort by size, biggest last.
alias lt='ls -ltr' # Sort by date, most recent last.
alias lc='ls -ltcr' # Sort by/show change time,most recent last.
alias lu='ls -ltur' # Sort by/show access time,most recent last.
# The ubiquitous 'll': directories first, with alphanumeric sorting:
alias ll="ls -lv --group-directories-first"
alias lm='ll |more' # Pipe through 'more'
alias lr='ll -R' # Recursive ls.
alias la='ll -A' # Show hidden files.
alias tree='tree -Csuh' # Nice alternative to 'recursive ls' ...
#-------------------------------------------------------------
# Tailoring 'less'
#-------------------------------------------------------------
alias more='less'
export PAGER=less
export LESSCHARSET='latin1'
export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-'
# Use this if lesspipe.sh exists.
export LESS='-i -N -w -z-4 -g -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'
# LESS man page colors (makes Man pages more readable).
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
#-------------------------------------------------------------
# Spelling typos - highly personnal and keyboard-dependent :-)
#-------------------------------------------------------------
alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'
#-------------------------------------------------------------
# A few fun ones
#-------------------------------------------------------------
# Adds some text in the terminal frame (if applicable).
xtitle()
{
case "$TERM" in
*term* | rxvt)
echo -en "\e]0;$*\a" ;;
*) ;;
esac
}
# Aliases that use xtitle
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'
# .. and functions
man()
{
for i ; do
xtitle The $(basename $1|tr -d .[:digit:]) manual
command man -a "$i"
done
}
#-------------------------------------------------------------
# Make the following commands run in background automatically:
#-------------------------------------------------------------
te() # wrapper around xemacs/gnuserv
{
if [ "$(gnuclient -batch -eval t 2>&-)" == "t" ]; then
gnuclient -q "$@";
else
( xemacs "$@" &);
fi
}
#-------------------------------------------------------------
# File & strings related functions:
#-------------------------------------------------------------
# Find a file with a pattern in name:
ff() { find . -type f -iname '*'"$*"'*' -ls ; }
# Find a file with pattern $1 in name and Execute $2 on it:
fe() { find . -type f -iname '*'"${1:-}"'*' \
-exec ${2:-file} {} \; ; }
# Find a pattern in a set of files and highlight them:
#+ (needs a recent version of egrep).
fstr()
{
OPTIND=1
local mycase=""
local usage="fstr: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
while getopts :it opt
do
case "$opt" in
i) mycase="-i " ;;
*) echo "$usage"; return ;;
esac
done
shift $(( $OPTIND - 1 ))
if [ "$#" -lt 1 ]; then
echo "$usage"
return;
fi
find . -type f -name "${2:-*}" -print0 | \
xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more
}
swap()
{ # Swap 2 filenames around, if they exist (from Uzi's bashrc).
local TMPFILE=tmp.$$
[ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
[ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
[ ! -e $2 ] && echo "swap: $2 does not exist" && return 1
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
}
extract() # Handy Extract Program
{
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
}
# Creates an archive (*.tar.gz) from given directory.
maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
# Create a ZIP archive of a file or folder.
makezip() { zip -r "${1%%/}.zip" "$1" ; }
# Make your directories and files access rights sane.
sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}
#-------------------------------------------------------------
# Process/system related functions:
#-------------------------------------------------------------
my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }
killps() # kill by process name
{
local pid pname sig="-TERM" # default signal
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: killps [-SIGNAL] pattern"
return;
fi
if [ $# = 2 ]; then sig=$1 ; fi
for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} )
do
pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
if ask "Kill process $pid <$pname> with signal $sig?"
then kill $sig $pid
fi
done
}
mydf() # Pretty-print of 'df' output.
{ # Inspired by 'dfc' utility.
for fs ; do
if [ ! -d $fs ]
then
echo -e $fs" :No such file or directory" ; continue
fi
local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
local out="["
for ((j=0;j<20;j++)); do
if [ ${j} -lt ${nbstars} ]; then
out=$out"*"
else
out=$out"-"
fi
done
out=${info[2]}" "$out"] ("$free" free on "$fs")"
echo -e $out
done
}
my_ip() # Get IP adress on ethernet.
{
MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' |
sed -e s/addr://)
echo ${MY_IP:-"Not connected"}
}
ii() # Get current host related info.
{
echo -e "\nYou are logged on ${BRed}$HOST"
echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
cut -d " " -f1 | sort | uniq
echo -e "\n${BRed}Current date :$NC " ; date
echo -e "\n${BRed}Machine stats :$NC " ; uptime
echo -e "\n${BRed}Memory stats :$NC " ; free
echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
echo -e "\n${BRed}Local IP Address :$NC" ; get-local-ip
echo -e "\n${BRed}Public IP Address :$NC" ; get-public-ip
# if [ $fucks_given -lt 1 ] && hash ss 2>/dev/null; then echo -e "\n${BRed}Open connections (SS):$NC "; ss -s; fi
echo
}
#-------------------------------------------------------------
# Misc utilities:
#-------------------------------------------------------------
repeat() # Repeat n times command.
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do # --> C-like syntax
eval "$@";
done
}
ask() # See 'killps' for example of use.
{
echo -n "$@" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
corename() # Get name of app that created a corefile.
{
for file ; do
echo -n $file : ; gdb --core=$file --batch | head -1
done
}
#=========================================================================
#
# PROGRAMMABLE COMPLETION SECTION
# Most are taken from the bash 2.05 documentation and from Ian McDonald's
# 'Bash completion' package (http://www.caliban.org/bash/#completion)
# You will in fact need bash more recent then 3.0 for some features.
#
# Note that most linux distributions now provide many completions
# 'out of the box' - however, you might need to make your own one day,
# so I kept those here as examples.
#=========================================================================
if [ "${BASH_VERSION%.*}" \< "3.0" ]; then
echo "You will need to upgrade to version 3.0 for full \
programmable completion features"
return
fi
shopt -s extglob # Necessary.
complete -A hostname rsh rcp telnet rlogin ftp ping disk
complete -A export printenv
complete -A variable export local readonly unset
complete -A enabled builtin
complete -A alias alias unalias
complete -A function function
complete -A user su mail finger
complete -A helptopic help # Currently same as builtins.
complete -A shopt shopt
complete -A stopped -P '%' bg
complete -A job -P '%' fg jobs disown
complete -A directory mkdir rmdir
complete -A directory -o default cd
# Compression
complete -f -o default -X '*.+(zip|ZIP)' zip
complete -f -o default -X '!*.+(zip|ZIP)' unzip
complete -f -o default -X '*.+(z|Z)' compress
complete -f -o default -X '!*.+(z|Z)' uncompress
complete -f -o default -X '*.+(gz|GZ)' gzip
complete -f -o default -X '!*.+(gz|GZ)' gunzip
complete -f -o default -X '*.+(bz2|BZ2)' bzip2
complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2
complete -f -o default -X '!*.+(zip|ZIP|z|Z|gz|GZ|bz2|BZ2)' extract
# Documents - Postscript,pdf,dvi.....
complete -f -o default -X '!*.+(ps|PS)' gs ghostview ps2pdf ps2ascii
complete -f -o default -X \
'!*.+(dvi|DVI)' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.+(pdf|PDF)' acroread pdf2ps
complete -f -o default -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?\
(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps
complete -f -o default -X \
'!*.+(doc|DOC|xls|XLS|ppt|PPT|sx?|SX?|csv|CSV|od?|OD?|ott|OTT)' soffice
# Multimedia
complete -f -o default -X \
'!*.+(gif|GIF|jp*g|JP*G|bmp|BMP|xpm|XPM|png|PNG)' xv gimp ee gqview
complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321
complete -f -o default -X '!*.+(ogg|OGG)' ogg123
complete -f -o default -X \
'!*.@(mp[23]|MP[23]|ogg|OGG|wav|WAV|pls|\
m3u|xm|mod|s[3t]m|it|mtm|ult|flac)' xmms
complete -f -o default -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|\
asf|vob|VOB|bin|dat|vcd|ps|pes|fli|viv|rm|ram|yuv|mov|MOV|qt|\
QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX)' xine
complete -f -o default -X '!*.pl' perl perl5
# This is a 'universal' completion - it works when commands have
#+ a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
# Needs the '-o' option of grep
#+ (try the commented-out version if not available).
# First, remove '=' from completion word separators
#+ (this will allow completions like 'ls --color=auto' to work correctly).
COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
_get_longopts()
{
#$1 --help | sed -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \
#grep ^"$2" |sort -u ;
$1 --help | grep -o -e "--[^[:space:].,]*" | grep -e "$2" |sort -u
}
_longopts()
{
local cur
cur=${COMP_WORDS[COMP_CWORD]}
case "${cur:-*}" in
-*) ;;
*) return ;;
esac
case "$1" in
\~*) eval cmd="$1" ;;
*) cmd="$1" ;;
esac
COMPREPLY=( $(_get_longopts ${1} ${cur} ) )
}
complete -o default -F _longopts configure bash
complete -o default -F _longopts wget id info a2ps ls recode
_tar()
{
local cur ext regex tar untar
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# If we want an option, return the possible long options.
case "$cur" in
-*) COMPREPLY=( $(_get_longopts $1 $cur ) ); return 0;;
esac
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $( compgen -W 'c t x u r d A' -- $cur ) )
return 0
fi
case "${COMP_WORDS[1]}" in
?(-)c*f)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
+([^Izjy])f)
ext='tar'
regex=$ext
;;
*z*f)
ext='tar.gz'
regex='t\(ar\.\)\(gz\|Z\)'
;;
*[Ijy]*f)
ext='t?(ar.)bz?(2)'
regex='t\(ar\.\)bz2\?'
;;
*)
COMPREPLY=( $( compgen -f $cur ) )
return 0
;;
esac
if [[ "$COMP_LINE" == tar*.$ext' '* ]]; then
# Complete on files in tar file.
#
# Get name of tar file from command line.
tar=$( echo "$COMP_LINE" | \
sed -e 's|^.* \([^ ]*'$regex'\) .*$|\1|' )
# Devise how to untar and list it.
untar=t${COMP_WORDS[1]//[^Izjyf]/}
COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \
2>/dev/null ) )" -- "$cur" ) )
return 0
else
# File completion on relevant files.
COMPREPLY=( $( compgen -G $cur\*.$ext ) )
fi
return 0
}
complete -F _tar -o default tar
_make()
{
local mdef makef makef_dir="." makef_inc gcmd cur prev i;
COMPREPLY=();
cur=${COMP_WORDS[COMP_CWORD]};
prev=${COMP_WORDS[COMP_CWORD-1]};
case "$prev" in
-*f)
COMPREPLY=($(compgen -f $cur ));
return 0
;;
esac;
case "$cur" in
-*)
COMPREPLY=($(_get_longopts $1 $cur ));
return 0
;;
esac;
# ... make reads
# GNUmakefile,
# then makefile
# then Makefile ...
if [ -f ${makef_dir}/GNUmakefile ]; then
makef=${makef_dir}/GNUmakefile
elif [ -f ${makef_dir}/makefile ]; then
makef=${makef_dir}/makefile
elif [ -f ${makef_dir}/Makefile ]; then
makef=${makef_dir}/Makefile
else
makef=${makef_dir}/*.mk # Local convention.
fi
# Before we scan for targets, see if a Makefile name was
#+ specified with -f.
for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
if [[ ${COMP_WORDS[i]} == -f ]]; then
# eval for tilde expansion
eval makef=${COMP_WORDS[i+1]}
break
fi
done
[ ! -f $makef ] && return 0
# Deal with included Makefiles.
makef_inc=$( grep -E '^-?include' $makef |
sed -e "s,^.* ,"$makef_dir"/," )
for file in $makef_inc; do
[ -f $file ] && makef="$makef $file"
done
# If we have a partial word to complete, restrict completions
#+ to matches of that word.
if [ -n "$cur" ]; then gcmd='grep "^$cur"' ; else gcmd=cat ; fi
COMPREPLY=( $( awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \
{split($1,A,/ /);for(i in A)print A[i]}' \
$makef 2>/dev/null | eval $gcmd ))
}
complete -F _make -X '+($*|*.[cho])' make gmake pmake
_killall()
{
local cur prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# Get a list of processes
#+ (the first sed evaluation
#+ takes care of swapped out processes, the second
#+ takes care of getting the basename of the process).
COMPREPLY=( $( ps -u $USER -o comm | \
sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
awk '{if ($0 ~ /^'$cur'/) print $0}' ))
return 0
}
complete -F _killall killall killps
# MOTD AT LAUNCH OF TERMINAL/SOURCE/SSH CONNECTION
echo -e "${BBlue}Bash Version : ${BRed}${BASH_VERSION%.*}${BBlue} - DISPLAY on ${BRed}$DISPLAY${NC}"
echo -e "${BBlue}Local IP : ${BRed}$(get-local-ip)${BBlue}\nPublic IP: ${BRed}$(get-public-ip)${NC}"
date
# Local Variables:
# mode:shell-script
# sh-shell:bash
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment