# -*-Shell-script-*-
#########################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the version 3 of the GNU General Public License #
# as published by the Free Software Foundation. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
# Written by and Copyright (C) Francois Fleuret #
# Contact for comments & bug reports #
#########################################################################
# The site-specific and confidential settings are in another file
PRIVATE_BASHRC="${HOME}/private/bashrc.perso"
# If the MANPATH is not set, set it
[ "${MANPATH}" ] || MANPATH=$(manpath)
# If the private bashrc exists, execute it
[ -f "${PRIVATE_BASHRC}" ] && source "${PRIVATE_BASHRC}"
# !!! THIS HAS TO BE HERE EVEN IN THE NON-INTERACTIVE PART OR YOU WILL
# LOSE YOU PREVIOUS HISTORY !!!
export HISTFILESIZE=20000
export HISTSIZE=${HISTFILESIZE}
export HISTIGNORE="${HISTIGNORE}:&:[ ]*"
# I want to save the command time, but I do not want to see it in
# history
export HISTTIMEFORMAT=""
shopt -s histappend
# I realized that most of my settings are meaningful only in
# interactive mode. This should maybe be done more properly through
# using different .bash_profile and .bash_login
[[ ${TERM} == "dumb" ]] || [ ! -t 0 ] && return
######################################################################
## The interactive part
export VT_RESET=$'\033[0m'
export VT_BOLD=$'\033[1m'
export VT_UNDERLINE=$'\033[4m'
export VT_BLINK=$'\033[5m'
export VT_SET_TITLE=$'\033]0;'
export VT_END_TITLE=$'\007'
export VT_BLACK_FG=$'\033[30m'
export VT_RED_FG=$'\033[31m'
export VT_GREEN_FG=$'\033[32m'
export VT_YELLOW_FG=$'\033[33m'
export VT_BLUE_FG=$'\033[34m'
export VT_MAGENTA_FG=$'\033[35m'
export VT_CYAN_FG=$'\033[36m'
export VT_WHITE_FG=$'\033[37m'
export VT_BLACK_BG=$'\033[40m'
export VT_RED_BG=$'\033[41m'
export VT_GREEN_BG=$'\033[42m'
export VT_YELLOW_BG=$'\033[43m'
export VT_BLUE_BG=$'\033[44m'
export VT_MAGENTA_BG=$'\033[45m'
export VT_CYAN_BG=$'\033[46m'
export VT_WHITE_BG=$'\033[47m'
# This prevents ^S from freezing the shell
stty -ixon
alias rm='rm -i'
alias mv='mv -i'
# alias chmod='chmod -v'
alias chmod='chmod -c'
alias cp='cp -i'
alias rd=rmdir
alias md=mkdir
alias ps='ps uxaf'
alias df='df -hT --sync'
alias grep='grep -E --mmap'
alias s='screen -d -R -U && clear'
alias mc='echo Try mv ' # I'm fed up with midnight commander
alias kj="keyjnote -s -D 1000 -t Crossfade -T 100"
# alias fdupes='fdupes -r .'
# ls colors
if [ -e "${HOME}/.dircolors" ]; then
eval $(dircolors "${HOME}/.dircolors")
alias ls='ls --color'
# alias ll='ls --color -lth'
alias ll='ls --color -goh --time-style="+%Y %b %d %H:%M"'
alias l='ls --color -I "*~" -I "*.o"'
alias less='less -R'
else
# alias ll='ls -lth'
alias ll='ls -goh --time-style="+%Y %b %d %H:%M"'
alias l='ls -I "*~" -I "*.o"'
fi
export EDITOR=emacsclient
export GIT_EDITOR=${EDITOR}
######################################################################
# Ignored extensions when completing
export FIGNORE="CVS"
######################################################################
# Functions
# Find a file containing a name
function fn () {
name=$1
shift
find $* -name "*${name}*";
}
alias trash=trash.sh
######################################################################
# http://www.reddit.com/r/linux/comments/akt3j/a_functional_programming_style_map_function_for/
function map () {
local command i rep
if [ $# -lt 2 ] || [[ ! "$@" =~ :[[:space:]] ]];then
echo "Invalid syntax." >&2; return 1
fi
until [[ $1 =~ : ]]; do
command="$command $1"; shift
done
command="$command ${1%:}"; shift
for i in "$@"; do
if [[ $command =~ \{\} ]];then
rep="${command//\{\}/\"$i\"}"
eval "${rep//\\/\\\\}"
else
eval "${command//\\/\\\\} \"${i//\\/\\\\}\""
fi
done
}
######################################################################
## A version of pho which stores the image numbers in environment
## variables
function pho () {
PHO_BIN=/usr/bin/pho
TEMP=$(mktemp /tmp/pho.XXXXXXX)
${PHO_BIN} $* | tee ${TEMP}
PHO_NOTE_1=$(grep ^"Note 1: " ${TEMP} | sed -e "s/^[^:]*: //")
PHO_NOTE_2=$(grep ^"Note 2: " ${TEMP} | sed -e "s/^[^:]*: //")
PHO_NOTE_3=$(grep ^"Note 3: " ${TEMP} | sed -e "s/^[^:]*: //")
PHO_NOTE_R90=$(grep ^"Rotate 90 \(CW\): " ${TEMP} | sed -e "s/^[^:]*: //")
PHO_NOTE_R180=$(grep ^"Rotate 180: " ${TEMP} | sed -e "s/^[^:]*: //")
PHO_NOTE_R270=$(grep ^"Rotate -90 \(CCW\): " ${TEMP} | sed -e "s/^[^:]*: //")
\rm ${TEMP}
}
# function rotjpeg () {
# if [ $1 == "90" ] || [ $1 == "180" ] || [ $1 == "270" ]; then
# TEMP=$(mktemp /tmp/rotjpeg.XXXXXX)
# echo jpegtran -rotate $1 -copy all $2 > ${TEMP}
# echo cp $2 ${2/jpg/}original.jpg
# echo cp ${TEMP} $2
# rm ${TEMP}
# else
# echo "Can not rotate with an angle of $1 degrees."
# fi
# }
######################################################################
## A version of date that shows the time at home if TZ is set
function dt () {
echo "Local: $(date)"
if [[ ${TZ} ]]; then
unset TZ
echo "Home: $(date)"
fi
}
######################################################################
## ifup / ifdown with sudo and memorization of the network
## When invoked without an argument this "ifup" uses the same argument
## as the previous time
## When invoked without an argument this "ifdown" removes the last
## interface which was ifuped
[[ ${IFUPRC} ]] || IFUPRC="${HOME}/.ifuprc"
function ifup () {
echo "${VT_BOLD}${VT_GREEN_FG}This is the bash function ifup from .bashrc${VT_RESET}"
if [[ ! $* ]] && [[ -s ${IFUPRC} ]]; then
# If we have no argument and there is a .ifuprc, use it
ARGS=$(cat ${IFUPRC})
else
# Otherwise uses the given arguments, and store them
ARGS=$*
echo ${ARGS} > ${IFUPRC}
fi
echo "${VT_GREEN_FG}Running [sudo ifup ${ARGS}]${VT_RESET}"
sudo ifup ${ARGS}
# Ugly hack to remove the dsl modem dns server when we add
# explicitely a dns in the /etc/network/interfaces
REMOVE_LOCAL_DNS=/usr/local/bin/remove-local-dns.sh
if [[ -x ${REMOVE_LOCAL_DNS} ]]; then
echo "${VT_GREEN_FG}Running [sudo ${REMOVE_LOCAL_DNS} 192.168]${VT_RESET}"
sudo ${REMOVE_LOCAL_DNS} 192.168
fi
}
function ifdown () {
echo "${VT_BOLD}${VT_GREEN_FG}This is the bash function ifdown from .bashrc${VT_RESET}"
if [[ ! $* ]] && [[ -s ${IFUPRC} ]]; then
# If there are no arguments and there is a .ifuprc, get the
# interface from it
ARGS=$(cat ${IFUPRC} | sed -e "s/=.*$//")
else
# Otherwise, use the standard ifdown
ARGS=$*
fi
echo "${VT_GREEN_FG}Running sudo [ifdown ${ARGS}]${VT_RESET}"
sudo ifdown ${ARGS}
}
function checkgw () {
ping $(route -n | grep ^0.0.0.0 | awk '{print $2}')
}
######################################################################
# Show the most recent files, no scroll
function lr () {
HEIGHT=$(stty size | awk '{print $1}')
WIDTH=$(stty size | awk '{print $2}')
\ls -goth --time-style="+%Y %b %d %H:%M" $* | head -$((HEIGHT-2)) | cut -b1-${WIDTH}
# \ls -lth $* | head -$((HEIGHT-2)) | cut -b1-${WIDTH}
}
######################################################################
# cd and ls into a directory
# [from http://www.oreillynet.com/onlamp/blog/2007/01/whats_in_your_bash_history.html]
# function c () { cd "$@" && lr; }
######################################################################
# You can change the xterm background color on the fly!
function setxtermbg () {
echo -n $'\033]11;'$1$'\007'
}
######################################################################
# Shuffle the lines from the stdin
function shuffle () {
SEED=$1
[[ $SEED ]] || SEED=0
awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
}
######################################################################
# Stores the last entered command into a file
KEPT_COMMANDS=${HOME}/.kept_bash_commands
function keep () {
if [[ ${KEPT_COMMANDS} ]]; then
LINE=$(history | tail -2 | head -1 | sed -e "s/^[0-9 ]*//")
echo $LINE
echo $(date)": "${LINE} >> ${KEPT_COMMANDS}
else
echo "You have to set \$KEPT_COMMANDS"
fi
}
######################################################################
# I sometime burn CDs and DVDs
function burn () {
set -e
DEVICE="/dev/cdrw"
if [[ ! $1 ]]; then
echo "burn " >&2
elif [[ -f $1 ]]; then
if [[ $(file $1 | grep "ISO 9660") ]]; then
wodim -eject -v dev=${DEVICE} $1
else
echo "Unknown type of $1" >&2
fi
elif [[ -d $1 ]]; then
TMP=$(mktemp /tmp/cdimage.XXXXXX) && \
genisoimage -input-charset iso8859-1 -r -o ${TMP} $1 && \
wodim -eject -v dev=${DEVICE} ${TMP}
rm -f ${TMP}
else
echo "Can not find $1" >&2
fi
}
######################################################################
# And watch DVDs too!
function dvd () {
echo
echo " ! and @ Seek to the beginning of the previous/next chapter"
echo " j Cycle through the available subtitles"
echo " o Show/hide the timing"
echo
if [[ $1 ]]; then
dvd_device="$1"
shift
else
dvd_device="/dev/cdrom"
fi
title="1"
if [[ $1 ]]; then
title=$1
shift
fi
mplayer > /dev/null \
-stop-xscreensaver \
-vc ffmpeg12 -quiet \
-vf yadif \
-alang en \
-dvd-device ${dvd_device} dvd://${title}
# -slang en
}
function ripdvd () {
mkdir -p ${HOME}/dvds
cd ${HOME}/dvds
dvdbackup -v -M
eject
}
######################################################################
# Upload the sources from the current directory to work
function ulsrc () {
if [[ ! "${MY_WORK_MACHINE}" ]]; then
echo "\$MY_WORK_MACHINE undefined" 1>&2
return 1
fi
DIR=${PWD/$HOME\//}
scp {Makefile,*.{cc,h,sh}} ${MY_WORK_MACHINE}:${DIR}
echo "Uploaded to ${MY_WORK_MACHINE}:${DIR}/"
}
######################################################################
# Create small images from images
function mksmall () {
PARAMS="-geometry 800x600"
# Auto-orient does not seem to work at all, hence the ugly hack
# with exif below
# PARAMS="-auto-orient -geometry 800x600"
echo "Using ${PARAMS}"
DEST_DIR=$1
[[ ${DEST_DIR} ]] || DEST_DIR=./small
mkdir -p ${DEST_DIR}
if [[ ! -d ${DEST_DIR} ]]; then
echo "Can not create ${DEST_DIR}" >&2
return
fi
NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
NB=0
for i in $(find -maxdepth 1 -type f); do
if [[ -e ${DEST_DIR}/$i ]]; then
echo "The file ${DEST_DIR}/$i already exists."
else
orientation=$(exif $i \
| grep ^Orientation \
| head -1 \
| sed -e "s/^[^|]*|//" \
| sed -e "s/ *$//")
case ${orientation} in
"top - left")
rotation_cmd=""
;;
"right - top")
rotation_cmd="-rotate 90"
;;
"left - bottom")
rotation_cmd="-rotate 270"
;;
*)
rotation_cmd=""
echo "Unknown orientation \"${orientation}\" !"
;;
esac
if [[ -f ${i/JPG/MOV} ]]; then
CAPTION_PARAMS="-font vera-sans -pointsize 24 -fill white -annotate +10+32 Video"
else
CAPTION_PARAMS=""
fi
convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
fi
\ls -lt ${DEST_DIR}/$i
NB=$((NB+1))
echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
done
}
######################################################################
# Move a file to the ~/sources/config directory and replace it where
# it was by a symbolic link
function mvtoconfig () {
CONFIGDIR=${HOME}/sources/config
if [[ -d ${CONFIGDIR} ]]; then
NEWNAME=${CONFIGDIR}/$(basename $1 | sed -e "s/^\.//")
mv $1 $NEWNAME
ln -s $NEWNAME $1
else
echo "Can not find ${CONFIGDIR}"
fi
}
######################################################################
# Track uncommited files (I presume this is very ugly from a real git
# user perspective)
function git-fm () {
CURRENT_DIR=$(pwd)
NB_SUBDIR=0
for i in $(find -name ".git"); do
NB_SUBDIR=$((NB_SUBDIR+1))
cd ${CURRENT_DIR}/$(dirname $i)
NB_MODIFIED=$(git status | grep modified | wc -l)
if [[ ${NB_MODIFIED} -gt 0 ]]; then
echo "$(dirname $i) (${NB_MODIFIED})"
git status | grep modified \
| sed -e "s/^#\t/ /" | sed -e "s/modified: *//"
fi
done
cd ${CURRENT_DIR}
echo "Visited ${NB_SUBDIR} directories."
}
######################################################################
# Commits all directories under git
function git-ca () {
ORIGINAL_PWD=${PWD}
UNCOMMITTED=""
for d in $(find ${PWD} -name ".git" | sed -e "s/\.git$//"); do
cd $d
NB_MODIFIED=$(git status | grep modified | wc -l)
if [[ ${NB_MODIFIED} -gt 0 ]]; then
if [[ $(pwd) =~ ${NO_AUTOMATIC_GIT_COMMIT} ]]; then
UNCOMMITTED="${UNCOMMITTED} $(pwd)"
else
echo $(pwd)" (${NB_MODIFIED} modified file(s))"
git commit -a -m "Automatic commit" | grep -v ^#
fi
# git gc
fi
done
cd ${ORIGINAL_PWD}
if [[ ${UNCOMMITTED} ]]; then
echo "** WARNING: Did not automatically commit${UNCOMMITTED}"
fi
}
######################################################################
# Backups all git directories into an encrypted backup file located
# either on the usb key or the SD card (in that order) if they can be
# mounted.
function git-backup () {
BACKUPDIR=/mnt/key
mount ${BACKUPDIR} 2> /dev/null
if [[ ! $(mount | grep ${BACKUPDIR}) ]]; then
BACKUPDIR=/mnt/sd
mount ${BACKUPDIR}
fi
if [[ $(mount | grep ${BACKUPDIR}) ]]; then
echo "Mounted ${BACKUPDIR}"
else
echo "Could not mount the backup directory"
return 1
fi
RESULT=${BACKUPDIR}/gitbackup-$(date +%F-%H%M%S).tgz.mc
tar zcvf - $(find ${HOME}/ -name .git) \
| mcrypt -f ${HOME}/private/mcrypt.key > ${RESULT}
if [[ -f ${RESULT} ]]; then
ls -lh ${RESULT}
else
echo "Could not create the backup!"
return 1
fi
sync
umount ${BACKUPDIR} && echo "Umounted ${BACKUPDIR}"
}
######################################################################
# Downloads torrents located in ${BT_DIR}/torrents/ and puts the
# result in the ${BT_DIR}
function bt () {
if [[ ${BT_DIR} ]]; then
if [[ -d "${BT_DIR}/torrents" ]]; then
if [[ $1 ]]; then
mv $1 ${BT_DIR}/torrents
fi
if [[ "$(ps auxwww | grep btlaunchmanycurses | grep -v grep)" ]]; then
echo "A client is already running."
else
cd ${BT_DIR} && screen btlaunchmanycurses torrents --max_upload_rate 32
fi
else
echo "Directory ${BT_DIR}/torrents does not exist."
fi
else
echo "You have to set \$BT_DIR."
fi
}
######################################################################
# The complex prompt policy
export PS1
if [ "${CONSOLE}" == "yes" ]; then
PS1=""
else
# If the login is a standard one (as specified in
# IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
# show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
if [ ! ${IGNORED_PROMPT_LOGIN} ] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]; then
IDENT="${USER}"
fi
# If the display is not the main one, make the assumption that the
# shell is not running on the localhost, and show the hostname
[ "${DISPLAY}" != ":0.0" ] && IDENT="${IDENT}@\h"
# If there is the login or the hostname, add a ":" to the prompt
[ "${IDENT}" ] && IDENT="${IDENT}:"
# If we are root, show that in red
if [[ ${USER} == "root" ]]; then
PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
else
PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
fi
# In an xterm, show the hostname and path in the title bar, highlight
# the prompt
# [ "${TERMS_WITH_BAR}" ] || TERMS_WITH_BAR="^xterm|screen$"
# if [[ "${TERM}" =~ "${TERMS_WITH_BAR}" ]]; then
# PS1="\[${VT_SET_TITLE}shell@\h (\w)${VT_END_TITLE}${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
# else
# PS1="\[${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
# fi
fi
######################################################################
# This implements a local history. If we are in a directory containing
# a writable local history file, we add the last line of the global
# history to it.
LOCAL_HISTORY_FILE=".local_bash_history"
function keep_local_history () {
if [[ -w "${LOCAL_HISTORY_FILE}" ]]; then
history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
TMP=$(mktemp /tmp/lh.XXXXXX)
\chmod 600 ${TMP}
uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
# mv would replace a symbolic link, while cp keeps it
\cp ${TMP} ${LOCAL_HISTORY_FILE}
\rm ${TMP}
LOCAL_HISTORY_HINT=" LH "
else
LOCAL_HISTORY_HINT=""
fi
}
PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
######################################################################
# Switch off the history
alias nh=" export HISTFILE=/dev/null"
function histfile_cue () {
if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]; then
HISTORY_CUE="[${HISTFILE}]"
else
HISTORY_CUE=""
fi
}
PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
######################################################################
# The dus command is available on my web site
#
# git clone http://fleuret.org/git/dus/
alias dus='dus -f'
######################################################################
# The finddup command is available on my web site
#
# git clone http://fleuret.org/git/finddup/
alias finddup='finddup -p0d'
######################################################################
# This script grep messages in my mail archives
alias gma='gma.sh'
######################################################################
# Selector based history
#
# The selector command is available on my web site
#
# git clone http://fleuret.org/git/selector/
function selector-history () {
selector -c 7 4 0 3 -q -b -i -d -v -w -l 15000 <(history)
}
# M-r puts the selected history line in place of the current one
bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
# M-t appends the selected history line and the end of the current one
bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
# Find pathes in the history and make a list of the existing ones
function selector-cd () {
LIST_TEMP=$(mktemp /tmp/cdlist.XXXXXX)
for d in $(history | \
grep ^" *[0-9]* *cd" | \
awk '{ print $3 }' | \
grep -v "\.\." | \
uniq); do
if [[ -d $d ]]; then
echo "$d!cd $d"
fi
done >> ${LIST_TEMP}
selector -v -x '!' -d -i ${LIST_TEMP}
\rm ${LIST_TEMP}
}
bind '"\C-[c":"\C-a\C-kselector-cd\C-m"'
# And we avoid to put in the history the use of the selector, which we
# do too often
HISTIGNORE="${HISTIGNORE}:selector-history"
######################################################################
function prompt_command () {
# save the history after every command to avoid loosing some when
# multiple shells are open
history -a
# and the local histories system defined above
keep_local_history
# and the history cue
histfile_cue
}
PROMPT_COMMAND="prompt_command"
######################################################################
# Displaying the timezone if it is set
if [[ ${TZ} ]]; then
echo "${VT_BOLD}${VT_GREEN_FG}Time zone is ${TZ}.${VT_RESET}"
fi
######################################################################