时间 block 访问 字符 小写 username 普通用户 jobs 技术
五周第五次课(1 月 12 日)
8.10 shell 特殊符号 cut 命令
8.11 sort_wc_uniq 命令
8.12 tee_tr_split 命令
8.13 shell 特殊符号下
相关测验题目: http://ask.apelearn.com/question/5437
扩展
source exec 区别 http://alsww.blog.51cto.com/2001924/1113112
Linux 特殊符号大全 http://ask.apelearn.com/question/7720
sort 并未按 ASCII 排序 http://blog.csdn.net/zenghui08/article/details/7938975
一 特殊符号
[ ] * 任意个任意字符,通配符
[ ] ? 任意一个字符
[ ] # 注释字符
[ ] \ 脱义字符
[ ] | 管道符
二 几个和管道有关的命令
oot@localhost:~# a=1
root@localhost:~# b=2
root@localhost:~# c=$a$b
root@localhost:~# echo $c
12
root@localhost:~# c=\$a\$b
root@localhost:~# echo $c
$a$b
[ ] cut 分割,-d 分隔符 -f 指定段号 -c 指定第几个字
[ ] sort 排序, -n 以数字排序 -r 反序 -t 分隔符 -kn1/-kn1,n2
root@localhost:~# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
root@localhost:~# cat /etc/passwd |head -2 |cut -d ":" -f 1 //只取第一段
root
bin
root@localhost:~# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 //取第一段,第二段
root:x
bin:x
root@localhost:~# cat /etc/passwd |head -2 |cut -c 4
t
:
[ ] wc -l 统计行数 -m 统计字符数 -w 统计词
root@localhost: ~#sort - r / etc / passwd xavi: x: 1000 : 1000 : xavi,
xavi 's office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:993:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
test:x:1002:1002::/home/test:/bin/bash'
[ ] uniq 去重, -c 统计行数,文件内容是没有改变的
root@localhost:~# vi 2.txt
root@localhost:~# cat 2.txt
2221dedd
dede
ded
1213
32e4
ded
root@localhost:~# wc -l 2.txt
6 2.txt
root@localhost:~# wc -m 2.txt
32 2.txt
root@localhost:~# wc -w 2.txt
6 2.txt
[] tee 和> 类似,重定向的同时还在屏幕显示
root@localhost:~# sort 2.txt|uniq //必须先给文件排序,才能生效
1
11
1213
2
2221dedd
3
3232
32e4
444
ded
dede
root@localhost:~# sort 2.txt|uniq -c //统计重复次数,
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
[] te -a 和>> 类似, 追加重定向
root@localhost:~# echo "dadsaddsdad" |tee a.txt
dadsaddsdad
root@localhost:~# sort 2.txt|uniq -c |tee a.txt
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
[] tr 替换字符,tr'a''b', 大小写替换 tr '[a-z]' '[A-Z]' //[] 的意思是任选几个括号内的字符
root@localhost:~# sort 2.txt|uniq -c |tee -a a.txt
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
root@localhost:~# cat a.txt
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
[ ] split 切割,-b 大小(默认单位字节),-l 行数
[root@localhost ~]# echo "xavilinux" |tr '[al]' '[AL]'
xAviLinux
[root@localhost ~]# echo "xavilinux" |tr 'a' 'A'
xAvilinux
-b 表示依据大小来分割文档,单位为 byte
查看起实际大小,du -sh 查看的是块,du -sb 按照 byte 查找大小
[root@localhost ~]# mkdir split_dir
[root@localhost ~]# cd !$
cd split_dir
[root@localhost split_dir]# cp /etc/passwd ./
[root@localhost split_dir]# split -b 500 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac xad xae
如果 split 不指定目标文件名,则会以 xaa,xab...... 这样的文件名来存取切割后的文件.指定目标文件名,示例如下:
-l 表示依据行数来分割文档
[root@localhost split_dir]# rm -f x* //把前面分割好的文件全部删除
[root@localhost split_dir]# split -b 100 passwd 123
[root@localhost split_dir]# ls
123aa 123ad 123ag 123aj 123am 123ap 123as 123av passwd
123ab 123ae 123ah 123ak 123an 123aq 123at 123aw
123ac 123af 123ai 123al 123ao 123ar 123au 123ax
92 总用量
[root@localhost split_dir]# rm -f 123*
[root@localhost split_dir]# split -l 10 passwd //10行分割
[root@localhost split_dir]# wc -l *
46 passwd
10 xaa
10 xab
10 xac
10 xad
6 xae
三 shell 的特殊符号
1,特殊符号 $
$ 可以用作变量前面的标识符,还可以和!结合使用.
!$ 表示上条命令的最后一个变量,本例中上条命令最后是 1.txt,那么在当前命令下输入!$ 则表示 1.txt
[root@localhost /]# touch 1.txt
[root@localhost /]# ls 1.txt
1.txt
[root@localhost /]# ls !$
ls 1.txt
1.txt
2,特殊符号;
在一行命令中运行两个或两个以上的命令,需要在命令之间加符号;.
3,特殊符号~
[root@localhost /]# ls 1.txt ; wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1.txt;wc -l 2.txt //命令之间不空格也是有效的,
1.txt
8 2.txt
符号~ 代表用户的家目录,root 用户的家目录是 / root, 普通用户的家目录是 / home/username.
4,特殊符号 &
把一条命令放到后台执行,则需要加上符号 &,它通常用于命令运行时间较长的情况.比如,可以用在 sleep 后,如下所示:
[1]+ 已停止 wc -l(工作目录:~/split_dir)
[root@localhost /]# sleep 30 &
[2] 7047
[root@localhost /]# jobs
[2]- 运行中 sleep 30 &
5,重定向符号 >,>>,2 > 和 2>>,错误和正确都输入 &>
和 >> 分别表示取代和追加的意思.当我们运行一个命令报错时,报错信息会输出到当前屏幕.如果想重定向到一个文本,则要用重定向符号 2 > 或者 2>>,它们分别表示错误重定向和错误追加重定向.
6,中括号 [ ]
中括号内为字符组合,代表字符组合中的任意一个,还可以表示一个范围(1-3,a-z).
7,特殊符号 && ||
在上面刚刚提到了分号,用于多条命令间的分隔符.另外还有两个可以用于多条命令中间的特殊符号,那就是 "&&" 和 "||" 下面把这几种情况全列出:
使用 ";" 时,不管 command1 是否执行成功都会执行 command2;
command1 ; command2
command1 && command2
command1 || command2
使用 "&&" 时,只有 command1 执行成功后,command2 才会执行,否则 command2 不执行;
使用 "||" 时,command1 执行成功后 command2 不执行,否则去执行 command2,总之 command1 和 command2 总有一条命令会执行.
[root@localhost /]# ls 1a.txt; wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
[2]- 完成 sleep 30
ls: 无法访问1a.txt: 没有那个文件或目录
8 2.txt
[root@localhost /]# ls 1.txt || wc -l 2.txt
1.txt
[root@localhost /]# ls 1.txt && wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1a.txt && wc -l 2.txt
22.Shell 特殊符号和 cut,sort,wc,uniq,tee,tr,split 命令
来源: http://www.bubuko.com/infodetail-2461724.html