# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment and startup programs
if [ -f $HOME/shortcuts ]; then
source $HOME/shortcuts
fi
PATH=$HOME/bin:$PATH
export PATH
# User specific aliases and functions
alias 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 anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=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=yes
if [ -n "$force_color_prompt" ]; then
if [ -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=yes
else
color_prompt=
fi
fi
if [ "$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\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -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 more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias 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_aliases
fi
# 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; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# personal alias
alias 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.
if has("syntax")
syntax on
endif
"
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark
"
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
"
" 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 available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
"
"" 实用设置
"
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版本中的toolbar
set vb t_vb= "当vim进行编辑时,如果命令错误,会发出警报,该设置去掉警报
set ruler "在编辑过程中,在右下角显示光标位置的状态行
set nohls "默认情况下,寻找匹配是高亮度显示,该设置关闭高亮显示
set incsearch "在程序中查询一单词,自动匹配单词的位置;如查询desk单词,当输到/d时,会自动找到第一个d开头的单词,当输入到/de时,会自动找到第一个以ds开头的单词,以此类推,进行查找;当找到要匹配的单词时,别忘记回车
set backspace=2 "设置退格键可用
#!/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:"
date
exit 0
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
# 1
for ((i=1;i<=10;i++))
do
echo $(expr $i \* 3 + 1);
done
# 2
for i in $(seq 1 10)
do
echo $(expr $i \* 3 + 1);
done
# 3
for i in {1..10}
do
echo $(expr $i \* 3 + 1);
done
# 4
awk 'BEGIN{for(i=1; i<=10; i++) print i}'
exit 0
字符型循环
#!/bin/bash
# 1
for i in `ls`;
do
echo $i is file name\! ;
done
# 2
for i in $* ;
do
echo $i is input chart\! ;
done
# 3
for i in f1 f2 f3 ;
do
echo $i is appoint ;
done
# 4
list="rootfs usr data data2"
for i in $list;
do
echo $i is appoint ;
done
exit 0
路径查找型循环
#!/bin/bash
# 1
for file in /proc/*;
do
echo $file is file path \! ;
done
# 2
for file in $(ls *.sh)
do
echo $file is file path \! ;
done
exit 0
Linux下将文件夹命名为今天的日期的方法
alias today="date +F%" # +F% format is like 2020-01-01
mkdir results-$(today)
Linux下使ls命令只显示目录的方法
ls -F | grep '/$' #最易用,若将其结果保存在变量里,可用循环遍历并用cd访问
ls -l | grep '^d' #显示信息最完整
附:ls与cd连用示例
#!/bin/bash
dir=`ls -F | grep "/$"`
for i in $dir
do
cd $i
files=`ls` # or files=$(ls)
for j in $files
do
cat $j >> /home/test/share/all.out
done
cd ..
done
exit 0
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
echo "ccc" | sed 's/c*/aaa/g' #正确
echo "ccc" | sed 's/c\*/aaa/g' #错误
echo "ccc" | sed 's/c+/aaa/g' #错误
echo "ccc" | sed 's/c\+/aaa/g' #正确
Sed正则表达式详细介绍
行开始 ^
sed -n '/^The/p' books.txt
行尾 $
sed -n '/Coelho$/p' books.txt
单个字符 .
匹配除行字符结尾的任何单个字符
echo -e "cat\nbat\nrat\nmat\nbatting\nrats\nmats" | sed -n '/^..t$/p'
匹配字符集合 []
匹配这些字符,仅占一个位置
echo -e "Call\nTall\nBall" | sed -n '/[CT]all/ p'
不匹配字符集 [^]
echo -e "Call\nTall\nBall" | sed -n '/[^CT]all/ p'
字符范围 [-]
echo -e "Call\nTall\nBall" | sed -n '/[C-Z]all/ p'
零到一次出现 \?
匹配零次或一次其前面的字符
echo -e "Behaviour\nBehavior" | sed -n '/Behaviou\?r/ p'
一次或多次出现 \+
匹配一次或多次其前面的字符
echo -e "111\n22\n123\n234\n456\n222" | sed -n '/2\+/ p'
零或多次出现 *
匹配零次或多次其前面的字符
echo -e "ca\ncat" | sed -n '/cat*/ p'
n个重复 \{n\}
共出现n个其前面的字符
sed -n '/^[0-9]\{3\}$/p' numbers.txt
最少出现n个 \{n,\}
最少出现n个其前面的字符
sed -n '/^[0-9]\{5,\}$/p' numbers.txt
m到n次出现 \{m, n\}
sed -n '/^[0-9]\{5,8\}$/p' numbers.txt
或 \|\(\|\)
echo -e "str1\nstr2\nstr3\nstr4" | sed -n '/str\(1\|3\)/p'
echo -e "str1\nstr2\nstr3\nstr4" | sed -n '/str1\|3/p'
awk中的回溯引用 (back references)
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
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.
# 设置分隔符并读取(默认分隔符为空格、tab、换行符)
IFS=,
cat file.csv | while read a b c
do
echo $a $b $c
done
# 读取至一数组
IFS=,
cat file.csv | while read -a line
do
echo ${line[0]} ${line[1]} ${line[2]}
echo ${line[*]} # 输出数组全部元素
done
# 避免管道带来的变量赋值问题,且仅针对该文件读取设定IFS
while IFS=, read a b c
do
echo $a $b $c
done < file.csv
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 ), 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 for more information and examples.
Third answer: see for a detailed discussion of a framework for generalized text replacement, including an explanation on how to emulate backreferences (and much more) with awk.