售前咨询
技术支持
渠道合作

读《Linux Shell脚本攻略》第3章笔记

1. comm

comm命令可用于两个文件直接的比较。它有很多不错的选项可用来调整输出,以便我们执行交集、求差(difference)以及差集操作

交集:打印出两个文件所共有的行

求差:打印出知道文件所包含的且不相同的那些行

差集:打印出包含在文件A中,但不包含在其他指定文件中的那些行

需要注意的是comm必须使用排过序的文件作为输入

示例:

[root@server1 linuxeye]# cat A.txt

apple

orange

gold

silver

steel

iron

[root@server1 linuxeye]# cat B.txt

orange

gold

cookies

carrot

[root@server1 linuxeye]# sort A.txt -o A.txt

[root@server1 linuxeye]# sort B.txt -o B.txt

[root@server1 linuxeye]# comm A.txt B.txt

apple

carrot

cookies

gold

iron

orange

silver

steel

输出的第一列包含只在A.txt中出现的行,第二列包含只在B.txt中出现的行,第三列包含A.txt和B.txt中都出现的行

[root@server1 linuxeye]# comm A.txt B.txt -1 -2  #打印两个文件的交集,即删除第一列和第二列

orange

gold

[root@server1 linuxeye]# comm A.txt B.txt -3     #打印两个文件中不相同的行,即删除第三列

apple

carrot

cookies

iron

silver

steel

[root@server1 linuxeye]# comm A.txt B.txt -3 | sed ‘s/^\t//’

apple

carrot

cookies

iron

silver

steel

[root@server1 linuxeye]# comm A.txt B.txt -23     #A.txt的差集

apple

iron

silver

steel

[root@server1 linuxeye]# comm A.txt B.txt -13     #B.txt的差集

carrot

cookies

2. 查找并删除重复文件

73~75

3. 列举文件类型统计信息脚本

83

4. 环回文件与挂载

5. 生成ISO文件及混合ISO

mkisofs #用于创建ISO文件系统

mkisofs -V “label” -o test.iso ./test  #将整个目录的内容写入ISO文件中 -V指定ISO文件的卷标、-o指定ISO文件的路径

cdrecord  #可以将ISO文件刻录入CD ROM或DVD ROM

cdrecord -v dev=/dev/cdrom image.iso

-speed指定刻录速度

cdrecord -v dev=/dev/cdrom image.iso -speed 8 #参数8指定其刻录速度为8x

刻录CD ROM时也可以采用多区段(miltisession)方式,这样能在一张光盘上分多次刻录数据,参数-multi选项

cdrecord -v dev=/dev/cdrom image.iso -multi

eject  #弹出光驱托管

eject  #合上光驱托盘

6. 查找文件差异并进行修补

[root@server1 test]# cat version1.txt

this is the original text

line2

line3

line4

happy hacking!

[root@server1 test]# cat version2.txt

this is the original text

line2

line4

happy hacking!

GNU is not UNIX

[root@server1 test]# diff version1.txt version2.txt

3d2

< line3

5a5

GNU is not UNIX

[root@server1 test]# diff -u version1.txt version2.txt

— version1.txt        2012-12-10 15:10:27.000000000 +0800

+++ version2.txt        2012-12-10 15:11:00.000000000 +0800

@@ -1,5 +1,5 @@

this is the original text

line2

-line3

line4

happy hacking!

+GNU is not UNIX

-u用于生成一体化输出。因为一体化输出的可读性更好。+起始的是新加入的行,-起始的是删除的行

[root@server1 test]# diff -u version1.txt version2.txt > version.patch

现在可以用patch命令修改应用于任意一个文件。当应用于version1.txt时,我们就可以得到version2.txt;而当应用于version2.txt时,就可以得到version1.txt

[root@server1 test]# patch -p1 version1.txt < version.patch

missing header for unified diff at line 3 of patch

patching file version1.txt

[root@server1 test]# cat version1.txt

this is the original text

line2

line4

happy hacking!

GNU is not UNIX

下面的命令可以撤销做出的修改,使用-R选项,则不会提示用户y/n:

[root@server1 test]# patch -p1 version1.txt < version.patch

missing header for unified diff at line 3 of patch

patching file version1.txt

Reversed (or previously applied) patch detected!  Assume -R? [n] y

[root@server1 test]# cat version1.txt

this is the original text

line2

line3

line4

happy hacking!

# diff -Naur directory1  directory2

选项含义:

-N:将所有缺失的文件视为空文件

-a:将所有的文件视为文本文件

-u:生成一体化输出

-r:遍历目录下的所有文件

7. head tail

head命令总是读取输入文件的头部

打印前10行(不带参数默认10行):

[root@server1 test]# head /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

news:x:9:13:news:/etc/news:

指定打印前5行:

[root@server1 test]# head -n 5 /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

打印除最后N行之外的所有行:

[root@server1 test]# head -n -30 /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

打印最后10行(不带参数默认10行):

[root@server1 test]# tail /etc/passwd

xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

avahi-autoipd:x:100:104:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

mysql:x:500:500::/home/mysql:/sbin/nologin

www:x:501:501::/home/www:/sbin/nologin

tech:x:502:502::/home/tech:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

打印最后5行:

[root@server1 test]# tail -n 5 /etc/passwd

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

mysql:x:500:500::/home/mysql:/sbin/nologin

www:x:501:501::/home/www:/sbin/nologin

tech:x:502:502::/home/tech:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

打印除了前N行之外所有的行:

tail -n +(n+1)

打印除前30行之外的所有行,N+1=31:

[root@server1 test]# tail -n +31 /etc/passwd

haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

avahi-autoipd:x:100:104:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

mysql:x:500:500::/home/mysql:/sbin/nologin

www:x:501:501::/home/www:/sbin/nologin

tech:x:502:502::/home/tech:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

-f(–follow)

8. 只列出目录的其他方法

# ls -d */

# ls -F | grep “/$”

# ls -l | grep “^d”

# find . -type d -maxdepth 1 -print

9. pushd、popd快速定位

使用pushd、popd的时候,就可以无视cd命令了

压入并切换路径,使用:

[root@server1 ~]# pushd /var/log

/var/log ~

[root@server1 log]# pushd /usr/src

/usr/src /var/log ~

[root@server1 src]# pushd /etc

/etc /usr/src /var/log ~

[root@server1 etc]# pushd /dev

/dev /etc /usr/src /var/log ~

[root@server1 dev]# dirs

/dev /etc /usr/src /var/log ~

0    1      2        3    4

当你想切换到列表中任意一个路径时,将每条路径从0到n进行编号,然后使用你希望切换到的路径编号,如下:

[root@server1 dev]# pushd +3

/var/log ~ /dev /etc /usr/src

[root@server1 log]# pwd

/var/log

pushd总是将路径添加到栈,如果要从栈中删除路径,可以使用popd移除最近压入栈的路径并切换到下一个目录:

[root@server1 log]# popd

~ /dev /etc /usr/src

[root@server1 ~]#

[root@server1 ~]# popd +3

~ /dev /etc

# popd +no可以从列表中移除特定的路径,no是从左到右,从0到n开始计数的

10. 统计文本的行数、单词数和字符数

wc(Word Count单词统计)

[root@server1 test]# wc -l version.patch #统计行数

9 version.patch

[root@server1 test]# wc -w version.patch #统计单词书

28 version.patch

[root@server1 test]# wc -c version.patch #统计字符数

203 version.patch

[root@server1 test]# echo -n 1234 | wc -c # -n用于避免echo添加额外的换行符

4

[root@server1 test]# wc version.patch    #不使用任何执行wc会打印行数、单词数和字符数,彼此之间用制表符分隔

9  28 203 version.patch

[root@server1 test]# wc -L version.patch  #打印最长行的程度

59 version.patch

11. 打印目录树tree

只重点标记出匹配某种样式的文件

[root@server1 test]# tree ./ -P “*.txt” #用通配符描述样式

./

|– version1.txt

`– version2.txt

0 directories, 2 files

只重点标记出除符合某种样式之外ide那些文件

[root@server1 test]# tree ./ -I “*.txt”

./

|– filestat.sh

|– other

|– remove_duplicates.sh

|– test

|– test_copy1

|– test_copy2

|– version.patch

`– version1.txt.orig

0 directories, 8 files

tree命令可以生成html输出。如用下面的命令可以创建一个包含目录树输出的html文件

[root@server1 test]# tree ./ -H http://trustauth.cn -o out.html

上一篇:

下一篇:

相关新闻

 

领取优惠
免费预约

申请试用SSL证书

提交成功!

咨询客服