Chinaunix首页 | 论坛 | 博客
  • 博客访问: 690477
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c++,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

分类: LINUX

2013-05-15 22:05:40

    最近工作内容涉及一些shell的编写,下面记录些其过程中常用的一些内容,以备日后查询。

内容简要
FAQ1    变量$#,$@$0,$1...$n 的解释
FAQ2    重定向1>&2,2>&1
FAQ3    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/bash
echo "faq 01"
echo "$$"
echo "$?"
echo "$-"
echo "$*"
echo "$@"
echo "$#"
echo "$0"
echo "$1"
echo "$2"
exit $1
output:
[lee@bogon test]$ ./faq01 1 2 3
faq 01
5752
0
hB
1 2 3
1 2 3
3
./faq01
1
2
[lee@bogon test]$ echo $?
1


FAQ2    重定向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]$ ls
a.txt
[lee@bogon test]$ ls a.txt b.txt
ls: cannot access b.txt: No such file or directory
a.txt
提示错误,实际上是strandard error,下面重定向到文件中。
[lee@bogon test]$ ls a.txt b.txt 1>file.out 2>file.err
[lee@bogon test]$ cat file.err
ls: 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 directory
a.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 directory
a.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.txt
hello 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 context
hi!
行打印,使用方法: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 '$1 < 5 {print $1 "\t" $3}'
1    fall
[lee@bogon test]$ cat hello.txt | awk '$1 < 9 {print $1 "\t" $3}'
1    fall
8    fox
$1 < 5为条件,第一个参数小于5,则打印出来
条件也可以省去,直接打印内容
for example:
[lee@bogon test]$ cat hello.txt | awk '{print $1 "\t" $3}'
1    fall
8    fox

阅读(4572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~