# .bashrc# Source global definitionsif [ -f /etc/bashrc ]; then . /etc/bashrcfi#User specific environment and startup programsif [ -f $HOME/shortcuts ]; thensource $HOME/shortcutsfiPATH=$HOME/bin:$PATHexport PATH#User specific aliases and functionsalias qstat="qstat -u '*'"#alias screen="/usr/bin/screen -D -R"#alias rm="$HOME/bin/del.sh"#alias undel="$HOME/bin/del.sh -u"#alias ls="ls --color"alias ld="ls -d"alias c="clear"alias l="ls -alh"alias lf="ls -F|grep /"alias lt="ls -tlr"alias mv="mv -i"alias cp="cp -pi"alias diff="diff -b"#PERL5LIB=$MYHOME/perllib:$MYHOME/perllib/lib64/perl5/site_perl/5.8.5:$MYHOME/perlib/lib/perl5/site_perl/5.8.5#export PERL5LIB#export R_LIBS_USER=~/R:/data/apps/R_library
示例二
#~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)#for examples# If not running interactively, don't do anythingcase $- in *i*) ;; *) return;;esac# don't put duplicate lines or lines starting with space in the history.# See bash(1) formore optionsHISTCONTROL=ignoreboth# append to the history file, don't overwrite itshopt -s histappend#for setting history length see HISTSIZE and HISTFILESIZE inbash(1)HISTSIZE=1000HISTFILESIZE=2000# check the window size after each command and, if necessary,# update the values of LINES and COLUMNS.shopt -s checkwinsize# If set, the pattern "**" used in a pathname expansion context will# match all files and zero or more directories and subdirectories.#shopt -s globstar# make less more friendly for non-text input files, see lesspipe(1)[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"# 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# set a fancy prompt (non-color, unless we know we "want" color)case "$TERM"in xterm-color|*-256color) color_prompt=yes;;esac# uncomment for a colored prompt, if the terminal has the capability; turned# off by default to not distract the user: the focus in a terminal window# should be on the output of commands, not on the prompt#force_color_prompt=yesif [ -n "$force_color_prompt" ]; thenif [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then# We have color support; assume it's compliant with Ecma-48# (ISO/IEC-6429). (Lack of such support is extremely rare, and such# a case would tend to support setf rather than setaf.) color_prompt=yeselse color_prompt= fifiif [ "$color_prompt"= yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 'else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 'fiunset color_prompt force_color_prompt# If this is an xterm set the titleto user@host:dircase "$TERM"inxterm*|rxvt*) PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" ;;*) ;;esac# enable color support of ls and also add handy aliasesif [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto'#alias dir='dir --color=auto'#alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto'fi# colored GCC warnings and errors#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'# some morels aliasesalias ll='ls -alF'alias la='ls -A'alias l='ls -CF'# Add an "alert" alias for long running commands. Use like so:#sleep10; alertalias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.# You may want to put all your additions into a separate file like#~/.bash_aliases, instead of adding them here directly.# See /usr/share/doc/bash-doc/examples in the bash-doc package.if [ -f ~/.bash_aliases ]; then . ~/.bash_aliasesfi# enable programmable completion features (you don't need to enable# this, if it's already enabled in /etc/bash.bashrc and /etc/profile# sources /etc/bash.bashrc).if! shopt -oq posix; thenif [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fifi# personal aliasalias c="clear"alias ca="cat -A"
利用.vimrc个性化配置vim
复制一份vim配置模板到个人目录下
cp/usr/share/vim/vimrc~/.vimrc
利用vim编辑该文件
"" 系统自带文件关键内容如下"" Vim5 and later versions support syntax highlighting. Uncommenting the next" line enables syntax highlighting by default.ifhas("syntax")syntax onendif"" If using a dark background within the editing area and syntax highlighting" turn on this option as wellset background=dark"" Uncomment the following to have Vim jump to the last position when" reopening a fileifhas("autocmd") au BufReadPost * ifline("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endifendif"" Uncomment the following to have Vim load indentation rules and plugins" according to the detected filetype."if has("autocmd")" filetype plugin indent on"endif"" The following are commented out as they cause vim to behave a lot" differently from regular Vi. They are highly recommended though."set showcmd " Show (partial) command in status line."set showmatch " Show matching brackets."set ignorecase " Do case insensitive matching"set smartcase " Do smart case matching"set incsearch " Incremental search"set autowrite " Automatically save before commands like :next and :make"set hidden " Hide buffers when they are abandoned"set mouse=a " Enable mouse usage (all modes)"" Source a global configuration file if availableiffilereadable("/etc/vim/vimrc.local")source/etc/vim/vimrc.localendif""" 实用设置"set nocompatible"去掉有关vi一致性模式,避免以前版本的bug和局限set nu!"显示行号set guifont=Luxi/ Mono/9"设置字体,字体名称和字号filetype on "检测文件的类型set history=1000"记录历史的行数set background=dark "背景使用黑色syntax on"语法高亮度显示set autoindent"vim使用自动对齐,也就是把当前行的对齐格式应用到下一行(自动缩进)set cindent"cindent是特别针对C语言语法自动缩进set smartindent"依据上面的对齐格式,智能的选择对齐方式,对于类似C语言编写上有用set tabstop=4"设置tab键为4个空格set shiftwidth=4"设置当行之间交错时使用4个空格set ai!"设置自动缩进set showmatch"设置匹配模式,类似当输入一个左括号时会匹配相应的右括号set guioptions-=T "去除vim的GUI版本中的toolbarset vb t_vb="当vim进行编辑时,如果命令错误,会发出警报,该设置去掉警报set ruler"在编辑过程中,在右下角显示光标位置的状态行set nohls"默认情况下,寻找匹配是高亮度显示,该设置关闭高亮显示set incsearch "在程序中查询一单词,自动匹配单词的位置;如查询desk单词,当输到/d时,会自动找到第一个d开头的单词,当输入到/de时,会自动找到第一个以ds开头的单词,以此类推,进行查找;当找到要匹配的单词时,别忘记回车
set backspace=2"设置退格键可用
# in Ubuntusudoapt-yinstallrsyncsudoapt-yinstallcrontabs# in CentOSyum-yinstallrsyncyum-yinstallcrontabs
Prepare backup directory
mkdir/mac/backup
Prepare a backup script, for example, ~/backup.sh
#!/bin/bash#backup.sh#1. Local backup RSYNC="rsync --stats --compress --recursive --times --perms --links --delete --max-size=100M --exclude-from=/home/john/.rsync/exclude"
echo"1. Backup of /home/john start at:"date$RSYNC /home/john/data/ /mac/backup/echo"Backup end at:"date#2. Remote backup RSYNC="rsync --stats --compress --recursive --times --perms --links --delete --max-size=100M"echo"2. Backup 172.22.220.20:/data/ to /mac/backup2/ start at:"date$RSYNC john@172.22.220.20:/home/john/data/ /mac/backup2/echo"Backup end at:"dateexit0
Using crontab command to execute the backup script routinely, and record in a log file, for example, execute the command ~/backup.sh > ~/backup.log in 5:10am everyday:
add executable permission
chmod+x~/backup.sh
open crontab and edit it by the following command:
crontab-e# or crontab ~/cronjob
type in the following lines or write the following in a file (i.e. ~/crontab):
#!/bin/bash# 1for ((i=1;i<=10;i++))doecho $(expr $i \* 3+1);done# 2for i in $(seq110)doecho $(expr $i \* 3+1);done# 3for i in {1..10}doecho $(expr $i \* 3+1);done# 4awk'BEGIN{for(i=1; i<=10; i++) print i}'exit0
字符型循环
#!/bin/bash# 1for i in`ls`;doecho $i isfilename\! ;done# 2for i in $* ;doecho $i isinputchart\! ;done# 3for i inf1f2f3 ;doecho $i isappoint ;done# 4list="rootfs usr data data2"for i in $list;doecho $i isappoint ;doneexit0
system(cmd) 执行系统命令,返回退出码
mktime( YYYY MM dd HH MM ss [DST]) 生成时间格式
strftime(format,timestamp) 格式化时间输出,将时间戳转换为时间字符串
systime() 得到时间戳,返回从1970年1月1日开始到当前时间(不计闰年)的整秒数
test和[]中可用的比较运算符只有==和!=,两者都是用于字符串比较的,不可用于整数比较,整数比较只能使用-eq、-gt这种形式。无论是字符串比较还是整数比较都不支持大于号小于号。如果实在想用,对于字符串比较可以使用转义形式,如果比较"ab"和"bc":[ ab \< bc ],结果为真,也就是返回状态为0。[]中的逻辑与和逻辑或使用-a和-o表示。
if [ 'a' == 'b' ]; then
echo "a=b"
else
echo "a!=b"
fi
#result: a!=b
if [ 1 -lt 2 ]; then
echo '1<2'
else
echo '1>2'
fi
#result: 1<2
if [ 1 \< 2 ]; then
echo '1<2'
else
echo '1>2'
fi
#result: 1<2
if [[ abcdefg =~ ^[a-z0-9]+$ ]]; then
echo "match"
else
echo "no match"
fi
#result: match
if [[ "012345abc" =~ ^[0-9]+$ ]]; then
echo "match"
else
echo "no match"
fi
#result: no match
if ($i<5)
if [ $i -lt 5 ]
if [ $a -ne 1 -a $a != 2 ]
if [ $a -ne 1] && [ $a != 2 ]
if [[ $a != 1 && $a != 2 ]]
for i in $(seq 0 4); do echo $i; done
for i in `seq 0 4`; do echo $i; done
for ((i=0;i<5;i++)); do echo $i; done
for i in {0..4}; do echo $i; done
split [OPTION]... [FILE [PREFIX]]
-l 指定行数,每个文件多少行
-b 指定大小,每个文件100M,这种可能会破坏一行的完整性
-d 指定用数字递增为生成的文件名编号,最后以test为前缀
-a 指定有几位数字作为后缀,这里指定了2位,则从00开始
split -l 7000 1.csv -d -a 2 test
split -b 100M 1.csv -d -a 2 test
The usual (and correct) answer for backreferences in awk is: "you can't do backreferences in awk". That is only partly true.
If you need to match a pattern using a regular expression with backreferences, like what you do in sed:
sed -n '/\(foo\)\(bar\).*\2\1/p' # prints lines with "foobar" and "barfoo" later in the line
or similar things, then well, you can't do that easily with awk.
But if you are using backreferences during string substitution, to insert text previously captured by a capture group, then you will almost certainly be able to get what you want with awk. Following are some hints:
First and easiest answer (requires GNU awk): use gensub(). It supports backreferences natively. Example:
# reverse letter and following digit and insert "+" if letter is "a" or "c"
echo 'a1-b2-c3-a5-s6-a7-f8-e9-a0' | gawk '{print gensub(/([ac])([0-9])/,"\\2+\\1","g",$0)}'
# outcome: 1+a-b2-3+c-5+a-s6-7+a-f8-e9-0+a
Note that gensub(), unlike sub() and gsub(), returns the modified string without touching the original. Also note that the third parameter is much like sed's match number specification in the s/pattern/replacement/ command: it can either be a number, indicating to replace only that specific match, or the string "g" (as in the example), to indicate replacement of all matches. See the gawk manual for more information (including why backslashes must be escaped in the replacement text).
Second answer: sometimes you don't really need backreferences, since what you want can be accomplished without. Examples:
echo 'foo123bar' | sed 's/.*\([0-9]\{1,\}\).*/\1/'
echo 'blah <a href="http://some.site.tld/page1.html">blah blah</a>' | sed 's/.*"\([^"]*\)".*/\1/'
Both things can be done in awk (and sed as well!) without the need of backreferences. You just delete the part of the line you don't need:
awk '{gsub(/^[a-z]*|[a-z]*$/,""); print}' # 1st example
awk '{gsub(/^[^"]*"|"[^"]*$/,""); print}' # 2nd example
Generally speaking, however, the above methods (both sed and awk) require that you have only one matching substring to extract per line. For the same purpose, with some awks (see AwkFeatureComparison), you can use the possibility to assign a regexp to RS to "pull out" substrings from the input (and without the limitation of at most one match per line). See the last part of Pulling out things for more information and examples.
Third answer: see GeneralizedTextReplacement for a detailed discussion of a framework for generalized text replacement, including an explanation on how to emulate backreferences (and much more) with awk.
Here is an easy way to set different colours for different kinds of files when using the ls command.
Add the following lines to the bottom of your ~/.bashrc file:
alias ls='ls --color'
LS_COLORS='di=1:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35:*.rpm=90'
export LS_COLORS
The first line makes ls use the --color parameter by default, which tells ls to display files in different colours based on the setting of the LS_COLORS variable.
The second line is the tricky one, and what I have worked out so far has been by trial and error. The parameters (di, fi, etc.) refer to different Linux file types. I have worked them out as shown
di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket file
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a non-existent file (orphan)
mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
ex = file which is executable (ie. has 'x' set in permissions).
The *.rpm=90 parameter at the end tells ls to display any files ending in .rpm in the specified colour, in this case colour 90 (dark grey). This can be applied to any types of files (e.g. you could use *.png=35 to make jpeg files appear purple) As many or as few parameters as you like can go into the LS_COLORS variable, as long as the parameters are separated by colons.
Using trial and error (and a little bash script I wrote... my first one ever! :) I worked out all the colour codes, at least my interpretation of them
0 = default colour
1 = bold
4 = underlined
5 = flashing text
7 = reverse field
31 = red
32 = green
33 = orange
34 = blue
35 = purple
36 = cyan
37 = grey
40 = black background
41 = red background
42 = green background
43 = orange background
44 = blue background
45 = purple background
46 = cyan background
47 = grey background
90 = dark grey
91 = light red
92 = light green
93 = yellow
94 = light blue
95 = light purple
96 = turquoise
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background
These can even be combined, so that a parameter like di=5;31;42 in your LS_COLORS variable would make directories appear in flashing red text with a green background!
Setting LS_COLORS does more than just make your ls listings look pretty (although it certainly does do that), it is also very helpful in identifying files while wading through a file system.