#!/bin/bash # # This file is made by lantw44. # echo "Running .bash_include" # Internal Variables default_tty_setting=`stty -g` colorprompting='\[\e[1;31m\]\!\[\e[m\] [\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]\$ ' nocolorprompting='\! [\u@\h \w]\$ ' historycountfile="$HOME/.bash_history.count" historybackupfile="$HOME/.bash_history.bak" bgrunfiledir="$HOME/tmp/bgrun-`whoami`" trashdir="$HOME/trash" HISTSIZE=1000000 HISTFILESIZE=1000000 HISTCONTROL=ignoredups:ignorespace # Environment Variables export EDITOR=vim export FCEDIT=vim export PAGER=less # Aliases alias startcolor='PS1=$colorprompting' alias stopcolor='PS1=$nocolorprompting' alias ll='ls -l' alias grep='grep --color=always' alias rm='rm -i' alias cp='cp -pi' alias mv='mv -i' alias jobs='jobs -l' alias less='less -RS' alias cccc='LANG=C;LC_ALL=C' alias enus='LANG=en_US.UTF-8;LC_ALL=en_US.UTF-8' alias big5='LANG=zh_TW.Big5;LC_ALL=zh_TW.Big5' alias zhtw='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8' alias utf8='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8' alias savetty='default_tty_setting=`stty -g`' alias resetty='stty $default_tty_setting' # Functions function compile_all () { local noask=0 [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1 [ "$1" = "-n" ] && noask=1 if [ "$noask" = "0" ]; then # read -p "CFLAGS [$CFLAGS] ? " NEWCFLAGS # read -p "LDFLAGS [$LDFLAGS] ? " NEWLDFLAGS read -e -p "CFLAGS: " -i "$CFLAGS" NEWCFLAGS read -e -p "LDFLAGS: " -i "$LDFLAGS" NEWLDFLAGS [ "$NEWCFLAGS" '!=' '' ] && CFLAGS=$NEWCFLAGS [ "$NEWLDFLAGS" '!=' '' ] && LDFLAGS=$NEWLDFLAGS else shift fi while [ "$1" '!=' '' ] do local TARGETFILE="`echo "$1" | cut -d . -f 1`" local SUFFIX="`echo "$1" | cut -d . -f 2`" if [ -f "$1" ]; then true else printf\ '\e[1;33mWarning\e[0m: Non-existent file or not a regular file\n' shift ; continue fi [ "$TARGETFILE" = "$1" ] && shift && continue if [ "$SUFFIX" = "c" ]; then echo "[CC] $1 -> $TARGETFILE" gcc $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE" elif [ "$SUFFIX" = "cpp" ]; then echo "[CXX] $1 -> $TARGETFILE" g++ $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE" else printf 'Unknown suffix (\e[1;33mskipped\e[0m)\n' fi [ "$?" '!=' "0" ] && printf\ '\e[1;31mError\e[0m while compiling file\n' shift done return 0 } function convert_to_html () { while [ "$1" '!=' '' ] do for i in "$1" do vim $i -c 'set background=dark' \ -c 'highlight PreProc ctermfg=darkcyan' \ -c "$BEFORE_CONVERT_TO_HTML" \ -c TOhtml \ -c :w \ -c :qa done shift done } function mkscreenacl () { PERMIT_COMMAND="select windowlist other meta detach reset hardcopy info redisplay lastmsg next prev xon xoff windows suspend help colon copy paste writebuf readbuf displays stuff attach" while [ "$1" '!=' '' ] do for i in $PERMIT_COMMAND do echo "aclchg $1 +x $i" done echo "aclchg $1 -rw \"#?\"" shift done } ########## Background ########## alias bgr=bgrun alias bgv=bgview alias bgl=bglist alias bgc=bgcount alias bgls=bglist alias bgrm=bgclean function bgrun () { [ "$#" = "0" ] && return 1 [ '!' -d "$bgrunfiledir" ] && mkdir -p "$bgrunfiledir" local current_time=`date "+%Y%m%d-%H%M%S"` local cmdname=`echo "$1" | sed -e 's/-/_/g' -e 's/\\//_/g' -e 's/ //g'` if [ "`echo "$cmdname" | cut -c 1`" == "_" ] then cmdname=`echo "$cmdname" | cut -c 2-` fi local filename="$bgrunfiledir/$current_time-$cmdname" echo "Writing to $filename" echo "$@" > $filename "$@" &>> "$filename" & } function bglist () { local viewtime=0 [ "$1" = "--full" ] && viewtime=1 { for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort` do [ "$viewtime" == "1" ] && echo "$i" head -n 1 "$i" done } | { if [ "$viewtime" == "1" ] then local readstat=0 local -i i=1 while true do local dateandtime_long local cmdline read dateandtime_long read cmdline [ "$?" '!=' "0" ] && break local dateandtime=`basename "$dateandtime_long"` local part_year=`echo $dateandtime | cut -c 1-4` local part_month=`echo $dateandtime | cut -c 5-6` local part_date=`echo $dateandtime | cut -c 7-8` local part_hour=`echo $dateandtime | cut -c 10-11` local part_minute=`echo $dateandtime | cut -c 12-13` local part_second=`echo $dateandtime | cut -c 14-15` printf '%6d' "$i" echo " $part_year-$part_month-$part_date $part_hour:$part_minute:$part_second $cmdline" i=$i+1 done else cat -n fi } | $PAGER } function bgview () { local -i yourchoice if [ "$1" = "" ] then yourchoice=`bgcount` else if [ "$1" -le "0" ] then yourchoice=$((`bgcount`+$1)) else yourchoice=$1 fi fi echo "Your choice is $yourchoice." local realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${yourchoice}p` echo "Command Line: `head -n 1 "$realfilename"`" read -e -p "View '$realfilename' ? " confirm if [ "$confirm" = "n" ] || [ "$confirm" = "N" ] then return 1 fi { printf "Command Line: " cat "$realfilename" } | $PAGER } function bgcount () { find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}' } function bgclean () { while [ "$1" ] do if [ "$1" = "all" ] then echo "Removing the directory $bgrunfiledir" rm -rf "$bgrunfiledir" & return 0 fi local filename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n $1p` echo "Removing $filename" rm -f $filename shift done } ########## Background End ########## function check_dmesg () { [ "$#" = "0" ] && return 1 while true do PREVIOS_DMESG_BUF="$DMESG_BUF" DMESG_BUF="`dmesg`" [ "$PREVIOS_DMESG_BUF" '!=' "$DMESG_BUF" ] && [ "$FIRST_RUN" = "0" ] && echo '===> system message buffer was modified <===' sleep $1 [ "$?" '!=' "0" ] && return 1 FIRST_RUN=0 done } function prehistory_backup () { echo "Running prehistory_backup" local currentcount=`wc "$HISTFILE" | awk '{print $1}'` [ '!' -f "$historycountfile" ] && touch "$historycountfile" local -i previoushistorycount previoushistorycount=`cat "$historycountfile"` if [ "$currentcount" -lt "$previoushistorycount" ] then printf "\e[1;31mWarning\e[m: Your $HISTFILE may be MODIFIED BY OTHER PROGRAMS!\n" printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n" echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed." echo " 1. Check your $HISTFILE." echo " 2. Edit your $HISTFILE manually if some unexpected changes are found." echo " (You may need $historybackupfile to do it) " echo " 3. Remove the file $historycountfile." echo " 4. Run the command \`prehistory_backup' again." return 1 fi echo "$currentcount" > "$historycountfile" \cp -f "$HISTFILE" "$historybackupfile" } ########## Trash Manager ########## function trash_mv () { [ "$#" = "0" ] && return 1 [ '!' -d "$trashdir" ] && mkdir -p "$trashdir" local current_time=`date "+%Y%m%d-%H%M%S"` } function trash_rm () { echo bb } function trash_ls () { echo cc } function trash_view () { echo dd } function trash_du () { echo ee } ########## Trash Manager End ########## ########## Help ########## function help_function () { echo uu } ########## Help End ########## # Doing something startcolor prehistory_backup umask 0022 tty -s if [ "$?" = "0" ] then bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward' fi shopt -s histappend shopt -s checkwinsize shopt -s checkjobs shopt -s checkhash shopt -s cmdhist shopt -s mailwarn echo "Done" # check_dmesg.sh 5 &