sample-bashrc.html

来自「BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版」· HTML 代码 · 共 835 行 · 第 1/2 页

HTML
835
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>一个简单的.bashrc文件</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINKREL="HOME"TITLE="高级Bash脚本编程指南"HREF="index.html"><LINKREL="PREVIOUS"TITLE="历史命令"HREF="histcommands.html"><LINKREL="NEXT"TITLE="将DOS批处理文件转换为Shell脚本"HREF="dosbatch.html"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="histcommands.html"ACCESSKEY="P">前一页</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="dosbatch.html"ACCESSKEY="N">下一页</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="SAMPLE-BASHRC"></A>Appendix K. 一个简单的<TTCLASS="FILENAME">.bashrc</TT>文件</H1><P><TTCLASS="FILENAME">~/.bashrc</TT>文件决定了交互shell的行为.        好好的了解这个文件, 将会使你更加了解Bash. </P><P><AHREF="mailto:emmanuel.rouat@wanadoo.fr"TARGET="_top">Emmanuel	  Rouat</A>捐献了下边这个注释非常详细的<TTCLASS="FILENAME">.bashrc</TT>文件, 		  这个文件是为Linux系统编写的.         他希望读者能够给他一些回馈. </P><P>仔细的学习这个文件, 直到你可以自由重用其中的代码片断和函数, 		并把它们用到你自己的<TTCLASS="FILENAME">.bashrc</TT>文件中, 	   甚至可以放到你的脚本中. </P><DIVCLASS="EXAMPLE"><HR><ANAME="BASHRC"></A><P><B>例子 K-1. <TTCLASS="FILENAME">.bashrc</TT>文件样本</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#===============================================================  2&nbsp;#  3&nbsp;# 个人的$HOME/.bashrc文件, 基于bash-2.05a(或更高版本)  4&nbsp;#  5&nbsp;# 最后更新日期: 星期2 4月15 20:32:34 CEST 2003  6&nbsp;#  7&nbsp;# 这个文件(一般情况下)被只会被交互式shell读取.   8&nbsp;# 这里可以定义你的别名, 函数,   9&nbsp;# 和其他的一些交互式特征, 比如你的提示符.  10&nbsp;# 11&nbsp;# 这个文件(开始时)是为Solaris设计的,  12&nbsp;# 但是基于Redhat的默认.bashrc文件 13&nbsp;# --&#62; 为Linux修改.  14&nbsp;# 你在这里看到的大部分代码都是从网上找来的 15&nbsp;# (即internet).  16&nbsp;# 这个bashrc文件有点挤 -  17&nbsp;# 记住, 它仅仅是个例子而已. 按照你自己的需求进行裁减.  18&nbsp;# 19&nbsp;# 20&nbsp;#=============================================================== 21&nbsp; 22&nbsp;# --&#62; 注释由HOWTO的作者添加.  23&nbsp;# --&#62; 然后又被ER编辑了一下 :-) 24&nbsp; 25&nbsp;#-------------------------------------- 26&nbsp;# 如果有源代码的全局定义, 请在此处定义. 27&nbsp;#-------------------------------------- 28&nbsp; 29&nbsp;if [ -f /etc/bashrc ]; then 30&nbsp;        . /etc/bashrc   # --&#62; 读取/etc/bashrc, 如果存在的话.  31&nbsp;fi 32&nbsp; 33&nbsp;#------------------------------------------------------------- 34&nbsp;# $DISPLAY的自动设置 (如果还没设置的话) 35&nbsp;# 这用于linux - 可能运行的结果不同....  36&nbsp;# 问题是不同的终端种类对于'who am i'来说,  37&nbsp;# 将会给出不同的答案...... 38&nbsp;# 我还没发现一种'通用'方法 39&nbsp;#------------------------------------------------------------- 40&nbsp; 41&nbsp;function get_xserver () 42&nbsp;{ 43&nbsp;    case $TERM in 44&nbsp;	xterm ) 45&nbsp;            XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )  46&nbsp;            # Ane-Pieter Wieringa建议使用下面这种方式: 47&nbsp;            # I_AM=$(who am i) 48&nbsp;            # SERVER=${I_AM#*(} 49&nbsp;            # SERVER=${SERVER%*)} 50&nbsp; 51&nbsp;            XSERVER=${XSERVER%%:*} 52&nbsp;	    ;; 53&nbsp;	aterm | rxvt) 54&nbsp; 	# 找出一些运行在这里的代码..... 55&nbsp;	    ;; 56&nbsp;    esac   57&nbsp;} 58&nbsp; 59&nbsp;if [ -z ${DISPLAY:=""} ]; then 60&nbsp;    get_xserver 61&nbsp;    if [[ -z ${XSERVER}  || ${XSERVER} == $(hostname) || ${XSERVER} == "unix" ]]; then  62&nbsp;	DISPLAY=":0.0"		# 在本地主机上显示 63&nbsp;    else		 64&nbsp;	DISPLAY=${XSERVER}:0.0	# 在远端主机上显示 65&nbsp;    fi 66&nbsp;fi 67&nbsp; 68&nbsp;export DISPLAY 69&nbsp; 70&nbsp;#---------- 71&nbsp;# 一些设置 72&nbsp;#---------- 73&nbsp; 74&nbsp;ulimit -S -c 0		# 不需要任何coredump 75&nbsp;set -o notify 76&nbsp;set -o noclobber 77&nbsp;set -o ignoreeof 78&nbsp;set -o nounset 79&nbsp;#set -o xtrace          # 对于调试来说非常有用 80&nbsp; 81&nbsp;# 使能选项: 82&nbsp;shopt -s cdspell 83&nbsp;shopt -s cdable_vars 84&nbsp;shopt -s checkhash 85&nbsp;shopt -s checkwinsize 86&nbsp;shopt -s mailwarn 87&nbsp;shopt -s sourcepath 88&nbsp;shopt -s no_empty_cmd_completion  # 仅限于bash&#62;=2.04 89&nbsp;shopt -s cmdhist 90&nbsp;shopt -s histappend histreedit histverify 91&nbsp;shopt -s extglob	# 对于complete命令(按情况补全)来说是必要的 92&nbsp; 93&nbsp;# 禁用选项: 94&nbsp;shopt -u mailwarn 95&nbsp;unset MAILCHECK		# 当有邮件到达时, 我不希望我的shell提示我 96&nbsp; 97&nbsp; 98&nbsp;export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n' 99&nbsp;export HISTIGNORE="&#38;:bg:fg:ll:h"100&nbsp;export HOSTFILE=$HOME/.hosts	# 将远端主机的列表放入~/.hosts101&nbsp;102&nbsp;103&nbsp;104&nbsp;#-----------------------105&nbsp;# 问候, 问侯报文等等...106&nbsp;#-----------------------107&nbsp;108&nbsp;# 先定义一些颜色:109&nbsp;red='\e[0;31m'110&nbsp;RED='\e[1;31m'111&nbsp;blue='\e[0;34m'112&nbsp;BLUE='\e[1;34m'113&nbsp;cyan='\e[0;36m'114&nbsp;CYAN='\e[1;36m'115&nbsp;NC='\e[0m'              # 没有颜色116&nbsp;# --&#62; 很好. 与使用"ansi.sys"的DOS效果相同. 117&nbsp;118&nbsp;# 在黑色背景下看起来非常好.....119&nbsp;echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"120&nbsp;date121&nbsp;if [ -x /usr/games/fortune ]; then122&nbsp;    /usr/games/fortune -s     # 让我们的每天充满乐趣.... :-)123&nbsp;fi124&nbsp;125&nbsp;function _exit()	# 在退出shell时运行的函数126&nbsp;{127&nbsp;    echo -e "${RED}Hasta la vista, baby${NC}"128&nbsp;}129&nbsp;trap _exit EXIT130&nbsp;131&nbsp;#---------------132&nbsp;# Shell提示符133&nbsp;#---------------134&nbsp;135&nbsp;if [[ "${DISPLAY#$HOST}" != ":0.0" &#38;&#38;  "${DISPLAY}" != ":0" ]]; then  136&nbsp;    HILIT=${red}   # 远端主机: 提示符为红137&nbsp;else138&nbsp;    HILIT=${cyan}  # 本地主机: 提示符为青色139&nbsp;fi140&nbsp;141&nbsp;#  --&#62; 下面提示符函数中\W和\w的替换实例, 142&nbsp;#+ --&#62; 用来获得完整路径名的显示. 143&nbsp;144&nbsp;function fastprompt()145&nbsp;{146&nbsp;    unset PROMPT_COMMAND147&nbsp;    case $TERM in148&nbsp;        *term | rxvt )149&nbsp;            PS1="${HILIT}[\h]$NC \W &#62; \[\033]0;\${TERM} [\u@\h] \w\007\]" ;;150&nbsp;	linux )151&nbsp;	    PS1="${HILIT}[\h]$NC \W &#62; " ;;152&nbsp;        *)153&nbsp;            PS1="[\h] \W &#62; " ;;154&nbsp;    esac155&nbsp;}156&nbsp;157&nbsp;function powerprompt()158&nbsp;{159&nbsp;    _powerprompt()160&nbsp;    {161&nbsp;        LOAD=$(uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g")162&nbsp;    }163&nbsp;164&nbsp;    PROMPT_COMMAND=_powerprompt165&nbsp;    case $TERM in166&nbsp;        *term | rxvt  )167&nbsp;            PS1="${HILIT}[\A \$LOAD]$NC\n[\h \#] \W &#62; \[\033]0;\${TERM} [\u@\h] \w\007\]" ;;168&nbsp;        linux )169&nbsp;            PS1="${HILIT}[\A - \$LOAD]$NC\n[\h \#] \w &#62; " ;;170&nbsp;        * )171&nbsp;            PS1="[\A - \$LOAD]\n[\h \#] \w &#62; " ;;172&nbsp;    esac173&nbsp;}174&nbsp;175&nbsp;powerprompt     # 这是默认提示符 - 可能比较慢176&nbsp;                # 如果很慢的话, 可以使用fastprompt来代替....177&nbsp;178&nbsp;#===============================================================179&nbsp;#180&nbsp;# 别名和函数181&nbsp;#182&nbsp;# 事实上, 这里定义的一些函数非常大183&nbsp;# (比如'lowercase'), 但是我的机器是512M内存, 所以 .....184&nbsp;# 如果你想让这个文件小一点, 185&nbsp;# 可以将这些函数放到脚本中. 186&nbsp;#187&nbsp;# 其中的许多函数来自于bash-2.04188&nbsp;# 中的例子. 189&nbsp;#190&nbsp;#===============================================================191&nbsp;192&nbsp;#-------------------193&nbsp;# 个人的别名194&nbsp;#-------------------195&nbsp;196&nbsp;alias rm='rm -i'197&nbsp;alias cp='cp -i'198&nbsp;alias mv='mv -i'199&nbsp;# -&#62; 防止偶然的文件误操作. 200&nbsp;alias mkdir='mkdir -p'201&nbsp;202&nbsp;alias h='history'203&nbsp;alias j='jobs -l'204&nbsp;alias r='rlogin'205&nbsp;alias which='type -all'206&nbsp;alias ..='cd ..'207&nbsp;alias path='echo -e ${PATH//:/\\n}'208&nbsp;alias print='/usr/bin/lp -o nobanner -d $LPDEST'   # 假设LPDEST被定义209&nbsp;alias pjet='enscript -h -G -fCourier9 -d $LPDEST'  # 使用enscript的漂亮的打印210&nbsp;alias background='xv -root -quit -max -rmode 5'    # 将一张图片作为背景211&nbsp;alias du='du -kh'212&nbsp;alias df='df -kTh'213&nbsp;214&nbsp;# 'ls'家族 (假定使用GNU ls)215&nbsp;alias la='ls -Al'               # 显示隐藏文件216&nbsp;alias ls='ls -hF --color'	# 为识别的文件类型添加颜色217&nbsp;alias lx='ls -lXB'              # 按扩展名排序218&nbsp;alias lk='ls -lSr'              # 按尺寸排序219&nbsp;alias lc='ls -lcr'		# 按修改时间排序220&nbsp;alias lu='ls -lur'		# 按访问时间排序221&nbsp;alias lr='ls -lR'               # 递归ls222&nbsp;alias lt='ls -ltr'              # 按日期排序223&nbsp;alias lm='ls -al |more'         # 管道给'more'224&nbsp;alias tree='tree -Csu'		# 'ls'的另一种好方法225&nbsp;226&nbsp;# 裁减'less'227&nbsp;alias more='less'228&nbsp;export PAGER=less229&nbsp;export LESSCHARSET='latin1'230&nbsp;export LESSOPEN='|/usr/bin/lesspipe.sh %s 2&#62;&#38;-' # 如果lesspipe.sh存在, 就用这个231&nbsp;export LESS='-i -N -w  -z-4 -g -e -M -X -F -R -P%t?f%f \232&nbsp;:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'233&nbsp;234&nbsp;# 拼写错误 - 纯粹个人喜好 :-)235&nbsp;alias xs='cd'236&nbsp;alias vf='cd'237&nbsp;alias moer='more'238&nbsp;alias moew='more'239&nbsp;alias kk='ll'240&nbsp;241&nbsp;#----------------242&nbsp;# 一些有趣东西243&nbsp;#----------------244&nbsp;245&nbsp;function xtitle ()246&nbsp;{247&nbsp;    case "$TERM" in248&nbsp;        *term | rxvt)249&nbsp;            echo -n -e "\033]0;$*\007" ;;250&nbsp;        *)  251&nbsp;	    ;;252&nbsp;    esac253&nbsp;}254&nbsp;255&nbsp;# 别名...256&nbsp;alias top='xtitle Processes on $HOST &#38;&#38; top'257&nbsp;alias make='xtitle Making $(basename $PWD) ; make'258&nbsp;alias ncftp="xtitle ncFTP ; ncftp"259&nbsp;260&nbsp;# .. 和函数261&nbsp;function man ()262&nbsp;{263&nbsp;    for i ; do264&nbsp;	xtitle The $(basename $1|tr -d .[:digit:]) manual265&nbsp;	command man -F -a "$i"266&nbsp;    done267&nbsp;}268&nbsp;269&nbsp;function ll(){ ls -l "$@"| egrep "^d" ; ls -lXB "$@" 2&#62;&#38;-| egrep -v "^d|total "; }270&nbsp;function te()  # xemacs/gnuserv的包装器271&nbsp;{272&nbsp;    if [ "$(gnuclient -batch -eval t 2&#62;&#38;-)" == "t" ]; then273&nbsp;        gnuclient -q "$@";274&nbsp;    else275&nbsp;        ( xemacs "$@" &#38;);276&nbsp;    fi277&nbsp;}278&nbsp;279&nbsp;#---------------------------280&nbsp;# 与文件和字符串相关的函数:281&nbsp;#---------------------------282&nbsp;283&nbsp;# 使用名字模式来查找文件:284&nbsp;function ff() { find . -type f -iname '*'$*'*' -ls ; }285&nbsp;# 使用pattern $1和Execute $2来查找文件: 286&nbsp;function fe() { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \;  ; }287&nbsp;# 在一系列文件中找到模式, 并高亮288&nbsp;function fstr()289&nbsp;{290&nbsp;    OPTIND=1291&nbsp;    local case=""292&nbsp;    local usage="fstr: find string in files.293&nbsp;Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "294&nbsp;    while getopts :it opt

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?