标签:就是 slim blog 分割 题目 shell du -sh /etc/ 错误
五周第五次课(1月12日)8.10 shell特殊符号cut命令
8.11 sort_wc_uniq命令
8.12 tee_tr_split命令
8.13 shell特殊符号下
扩展
[ ] * 任意个任意字符,通配符
[ ] ? 任意一个字符
[ ] # 注释字符
[ ] \ 脱义字符
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
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
:
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
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
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
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
[ ] te -a 和>>类似,追加重定向
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
[root@localhost ~]# echo "xavilinux" |tr ‘[al]‘ ‘[AL]‘
xAviLinux
[root@localhost ~]# echo "xavilinux" |tr ‘a‘ ‘A‘
xAvilinux
-b 表示依据大小来分割文档,单位为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
查看起实际大小,du -sh查看的是块,du -sb 按照byte查找大小
如果split不指定目标文件名,则会以xaa、xab……这样的文件名来存取切割后的文件。指定目标文件名,示例如下:
[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
-l 表示依据行数来分割文档
[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
92 总用量
$可以用作变量前面的标识符,还可以和!结合使用。
[root@localhost /]# touch 1.txt
[root@localhost /]# ls 1.txt
1.txt
[root@localhost /]# ls !$
ls 1.txt
1.txt
!$表示上条命令的最后一个变量,本例中上条命令最后是1.txt,那么在当前命令下输入!$则表示1.txt
在一行命令中运行两个或两个以上的命令,需要在命令之间加符号;。
[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。
把一条命令放到后台执行,则需要加上符号&,它通常用于命令运行时间较长的情况。比如,可以用在sleep后,如下所示:
[root@localhost /]# sleep 30 &
[2] 7047
[root@localhost /]# jobs
[1]+ 已停止 wc -l(工作目录:~/split_dir)
[2]- 运行中 sleep 30 &
和>>分别表示取代和追加的意思。当我们运行一个命令报错时,报错信息会输出到当前屏幕。如果想重定向到一个文本,则要用重定向符号2>或者2>>,它们分别表示错误重定向和错误追加重定向。
中括号内为字符组合,代表字符组合中的任意一个,还可以表示一个范围(1-3,a-z)。
在上面刚刚提到了分号,用于多条命令间的分隔符。另外还有两个可以用于多条命令中间的特殊符号,那就是 “&&” 和 “||” 下面把这几种情况全列出:
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
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
ls: 无法访问1a.txt: 没有那个文件或目录
22.Shell特殊符号和cut,sort,wc,uniq,tee,tr,split命令
标签:就是 slim blog 分割 题目 shell du -sh /etc/ 错误
原文地址:http://blog.51cto.com/12995218/2060847