![yuyunliuhen yuyunliuhen]()
最近工作内容涉及一些shell的编写,下面记录些其过程中常用的一些内容,以备日后查询。内容简要FAQ1 变量$#,$@$0,$1...$n 的解释FAQ2 重定向1>&2,2>&1FAQ3 sed 工具FAQ4 awk工具FAQ1 变量$#,$@$0,$1...$n 的解释$$shell本身的pid;$!最后运行的命名行代码;$?最后运行的命令的结束代码;$-使用set命令设定的flag一览;$*代表『"$1 $2 $3 $4",中间默认为空格符;$@代表『"$1" "$2" "$3" "$4" 』,每个单独的变量用双引号括起来;$#shell脚本后面提供的参数个数,类似于c中的argc,一般用于判断参数个数是否符合;如:./clear_db root root databases那么$#的值即为3.$1,$2...$n 即代表具体的某个参数值;for example:#!/bin/bashecho "faq 01"echo "$$"echo "$?"echo "$-"echo "$*"echo "$@"echo "$#"echo "$0"echo "$1"echo "$2"exit $1output:[lee@bogon test]$ ./faq01 1 2 3faq 0157520hB1 2 31 2 33./faq0112[lee@bogon test]$ echo $?1FAQ2 重定向1>&2,2>&1在shell中,最常使用FD大概有三个,分别为0: standard input(键盘输入,并返回前端);1: strandard output(正确返回值,输出到前端);2: strandard error(错误返回值,输出道前端).for example:[lee@bogon test]$ mkdir test[lee@bogon test]$ cd test/[lee@bogon test]$ vi a.txt[lee@bogon test]$ lsa.txt[lee@bogon test]$ ls a.txt b.txtls: cannot access b.txt: No such file or directorya.txt提示错误,实际上是strandard error,下面重定向到文件中。[lee@bogon test]$ ls a.txt b.txt 1>file.out 2>file.err[lee@bogon test]$ cat file.errls: cannot access b.txt: No such file or directory[lee@bogon test]$ ls a.txt b.txt 1>file.out 2>&1[lee@bogon test]$ cat file.out ls: cannot access b.txt: No such file or directorya.txt[lee@bogon test]$ ls a.txt b.txt 2>file.err 1>&2[lee@bogon test]$ cat file.err ls: cannot access b.txt: No such file or directorya.txt由此可知,1>&2 正确返回值传递给2输出通道,&2表示2输出通道;同理,2>&1,错误返回值传递给1输出通道。另外:"1>" 通常可省略为">".FAQ3 sed 工具sed属于一种管道命令,可以用来分析standard input,而且可以将数据进行取代,删除,新增,撷取特定行功能。先说下最常用的功能,搜寻取代,使用方法sed 's/old_string/new_string/g' ,将new_string替换为old_string,加上g代表全部替换,否则仅仅替换一处。for example:[lee@bogon test]$ cat hello.txthello world![lee@bogon test]$ cat hello.txt |sed 's/hello/HELLO/g'HELLO world!删除,使用方法:sed 'n,md',n,m为文件行数,删除n~m行数内容。for example:[lee@bogon test]$ cat hello.txt hello world!hello!hi![lee@bogon test]$ cat hello.txt | sed '1,2d'hi!如果只删除一行,则可以sed 'nd',n为第几行;for example:[lee@bogon test]$ cat hello.txt | sed '1d'hello!hi!附加,使用方法:sed 'na context',n为第几行,即第几行后加内容,context即为加的内容。for example:[lee@bogon test]$ cat hello.txt | sed '2a why me?'hello world!hello!why me?hi!如果是加在第几行,而不是第几行之后,则可sed 'ni context',for example:[lee@bogon test]$ cat hello.txt | sed '2i why me?'hello world!why me?hello!hi!如果添加多行,则sed 'ni context1 \> context2 ' .for example:[lee@bogon test]$ cat hello.txt | sed '2a why me? \ > why? 'hello world!hello!why me? hi!行取代,使用方法:sed 'n,mc context',用context取代n到m行的内容,for example:[lee@bogon test]$ cat hello.txt | sed '1,2c 1~2 context'1~2 contexthi!行打印,使用方法:sed -n 'n,mp',打印n,m行的内容。for example:[lee@bogon test]$ cat hello.txt | sed -n '1,2p'hello world!hello!FAQ4 awk工具awk属于不错的数据处理工具,相对于sed处理整行,awk倾向于一行当中分数个字段处理,每行以空格或者TAB键分隔。使用方法:awk '条件类型1 {动作1} 条件类型2{动作2} 条件类型3 {动作3} ...... ' filename;for example:[lee@bogon test]$ cat hello.txt 1 A fall into a pit,a gain in your wit.8 A fox may grow gray,but never good.[lee@bogon test]$ cat hello.txt | awk '$11 fall[lee@bogon test]$ cat hello.txt | awk '$11 fall8 fox$1条件也可以省去,直接打印内容for example:[lee@bogon test]$ cat hello.txt | awk '{print $1 "\t" $3}'1 fall8 fox 12-17 00:13