众所周知,shell脚本是绝大多数linux高手的拿手活。在shell脚本中,通配符和重定向的使用很普遍。通配符的作用是使用一种表达式将所有符合的字符串表示出来。而重定向的作用是将运行命令后显示的内容输入输出重定向到另外一个文件或命令中。
1、通配符
|
1
2
3
4
5
6
7
8
9
10
|
[root@server01
test
]
# ls
10.txt 1.txt 2.txt 3.txt 4.txt a.txt b.txt c.txt d.txt e f g
[root@server01
test
]
# ls *.txt ##“*”表示匹配所有字符
10.txt 1.txt 2.txt 3.txt 4.txt a.txt b.txt c.txt d.txt
[root@server01
test
]
# ls ?.txt ##“?”表示匹配单个字符
1.txt 2.txt 3.txt 4.txt a.txt b.txt c.txt d.txt
[root@server01
test
]
# ls [0-9].txt ##“[]”表示匹配括号内的单个字符
1.txt 2.txt 3.txt 4.txt
[root@server01
test
]
# ls {1,2}.txt ##“{}”表示匹配花括号内的字符
1.txt 2.txt
|
2、重定向
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[root@server01
test
]
# cat 1.txt
This is from 1.TXT
[root@server01
test
]
# cat 1.txt > a.txt ##输出重定向
[root@server01
test
]
# cat a.txt
This is from 1.TXT
[root@server01
test
]
# cat 1.txt >> a.txt ##输出追加重定向
[root@server01
test
]
# cat a.txt
This is from 1.TXT
This is from 1.TXT
[root@server01
test
]
# ls aaa.txt 2> b.txt ##输出错误重定向
[root@server01
test
]
# cat b.txt
ls
: 无法访问aaa.txt: 没有那个文件或目录
[root@server01
test
]
# ls aaa.txt 2>> b.txt ##输出错误追加重定向
[root@server01
test
]
# cat b.txt
ls
: 无法访问aaa.txt: 没有那个文件或目录
ls
: 无法访问aaa.txt: 没有那个文件或目录
[root@server01
test
]
# wc -l < b.txt ##输入重定向
2
[root@server01
test
]
# ls [12].txt abc.txt > c.txt 2> d.txt #组合使用
[root@server01
test
]
# cat c.txt
1.txt
2.txt
[root@server01
test
]
# cat d.txt
ls
: 无法访问abc.txt: 没有那个文件或目录
[root@server01
test
]
# ls [12].txt abc.txt &> 4.txt ##无论正确错误都输出
[root@server01
test
]
# cat 4.txt
ls
: 无法访问abc.txt: 没有那个文件或目录
1.txt
2.txt
|
本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1942868,如需转载请自行联系原作者