首页 > 代码库 > linux 基础 shell脚本命令
linux 基础 shell脚本命令
#########shell脚本命令####
1.diff
diff file file1 ####比较两个文件的不同
-c ####显示周围的行
-u ####按照一格式统一输出生成补丁
-r ####比较两个文件的不同
patch file file.path ####打补丁
-b ####备份原文件
ot@desktop28 mnt]# diff westos westos.new -c ###显示周围行
*** westos2016-11-22 04:04:37.782657053 -0500
--- westos.new2016-11-22 04:05:02.900657053 -0500
***************
*** 1 ****
--- 1,4 ----
+ file
westos
+ linux
+
[root@desktop28 mnt]# diff westos westos.new -u ###显示详细情况
--- westos2016-11-22 04:04:37.782657053 -0500
+++ westos.new2016-11-22 04:05:02.900657053 -0500
@@ -1 +1,4 @@
+file
westos
+linux
+
[root@desktop28 mnt]# diff -u westos westos.new > westos.path ###生成补丁
[root@desktop28 mnt]# ll
total 12
-rw-r--r--. 1 root root 7 Nov 22 04:04 westos
-rw-r--r--. 1 root root 19 Nov 22 04:05 westos.new
-rw-r--r--. 1 root root 135 Nov 22 04:07 westos.path
[root@desktop28 mnt]# yum install patch -y ###安装打补丁软件
Loaded plugins: langpacks
Package patch-2.7.1-8.el7.x86_64 already installed and latest version
Nothing to do
[root@desktop28 mnt]# patch westos westos.path ###打补丁
patching file westos
[root@desktop28 mnt]# ll
total 12
-rw-r--r--. 1 root root 18 Nov 22 04:18 westos
-rw-r--r--. 1 root root 18 Nov 22 04:15 westos.new
-rw-r--r--. 1 root root 133 Nov 22 04:16 westos.path
[root@desktop28 mnt]# vim westos
[root@desktop28 mnt]# patch -b westos westos.new
patch: **** Only garbage was found in the patch input.
[root@desktop28 mnt]# patch -b westos westos.path
patching file westos
[root@desktop28 mnt]# ll
total 16
-rw-r--r--. 1 root root 18 Nov 22 04:19 westos
-rw-r--r--. 1 root root 18 Nov 22 04:15 westos.new
-rw-r--r--. 1 root root 7 Nov 22 04:18 westos.orig
-rw-r--r--. 1 root root 133 Nov 22 04:16 westos.path
2.grep
grep 关键字符 文件|目录 ###在文件或目录中查找含有关键字的行
grep -i ####忽略大小写
-n ###显示关键字所在的行
-c ###显示过滤结果的行数
-v ###反向过滤
-E “关键字1|关键字2” ###过滤多个关键字
-r 目录 ###在目录中查找含有关键字的文件
注意:^关键字 ###以关键字开头
关键字$ ###以关键字结尾
[root@localhost mnt]# grep root passwd ###找出passwd文件中含有root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
root:test:root
test:ROOT:root
[root@localhost mnt]# grep ^root passwd ##找出passwd文件中以root开头的行
root:x:0:0:root:/root:/bin/bash
root:test:root
[root@localhost mnt]# grep root$ passwd ##找出passwd文件中以root结尾的行
root:test:root
test:ROOT:root
[root@localhost mnt]# grep -i root passwd ##忽略大小写
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
root:test:root
test:ROOT:root
[root@localhost mnt]# grep -n root passwd ###找出关键字所在的行数
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
15:test:root:test
16:root:test:root
17:test:ROOT:root
[root@localhost mnt]# grep -v root$ passwd ##找出不以root结尾的行
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
test:root:test
[root@localhost mnt]# grep -i root passwd | grep -E "^root|root$" ##找出以root开头或结尾,不分大小写的行
root:x:0:0:root:/root:/bin/bash
root:test:root
test:ROOT:root
[root@localhost mnt]# grep -i root passwd | grep -E "^root|root$" -v ##找出不以root开头或结尾的行
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
[root@localhost mnt]# grep root -r /etc/ -n ###找出/etc/目录下含有root的文件并显示所在行数
/etc/pki/ca-trust/extracted/README:6:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:11:root CA certificates.
Binary file /etc/pki/ca-trust/extracted/java/cacerts matches
/etc/pki/ca-trust/extracted/openssl/README:12:root CA certificates.
/etc/pki/ca-trust/extracted/pem/README:15:root CA certificates.
/etc/pki/tls/certs/make-dummy-cert:11:echo root@localhost.localdomain
/etc/pki/tls/openssl.cnf:332:dir= ./demoCA# TSA root directory
###grep正则表达式###
3.cut
cut ###截取字符
cut -d 分隔符 ###指定分隔符
cut -c 1-4 ###显示指定的字符
cut -f 1,5 ###显示指定的列
[root@localhost mnt]# cat 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
test:root:test
root:test:root
test:ROOT:root
[root@localhost mnt]# cut -d ":" -f 1,3 passwd ##截取第一,三列
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
dbus:81
test:test
root:root
test:root
[root@localhost mnt]# cut -d ":" -f 1-3 passwd ##截取1到3列
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
ftp:x:14
nobody:x:99
dbus:x:81
test:root:test
root:test:root
test:ROOT:root
[root@localhost mnt]# cut -c 2-5 passwd ##截取第2到第5个字符的列
oot:
in:x
aemo
dm:x
p:x:
ync:
hutd
alt:
ail:
pera
ames
tp:x
obod
bus:
est:
oot:
est:
[root@localhost mnt]# cut -c 2,5 passwd ##截取第2,5个字符所在的列
o:
ix
ao
dx
p:
y:
hd
a:
a:
pa
as
tx
od
b:
e:
o:
e:
例子:
用命令, 只显示出eth0的ip.
[root@localhost mnt]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.28.11 netmask 255.255.255.0 broadcast 172.25.28.255
inet6 fe80::5054:ff:fe00:1c0b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:1c:0b txqueuelen 1000 (Ethernet)
RX packets 1899 bytes 165993 (162.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1244 bytes 292701 (285.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost mnt]# ifconfig eth0 | grep inet |grep inet6 -v| cut -d " " -f 10
172.25.28.11
或
[root@localhost mnt]# ifconfig eth0 | grep inet |grep inet6 -v| awk -F " " ‘{print $2}‘
172.25.28.11
#####sort#######
sort ####排序
-n ####纯数字排序
-u ####去冗余
-|uniq -c ####去冗余并统计冗余次数
-t ####值定分隔符
-k ####指定列
[root@localhost mnt]# cat westos
1
1
2
2
4
5
2
65
2
12
43
[root@localhost mnt]# sort westos ·###排序
1
1
12
2
2
2
2
4
43
5
65
[root@localhost mnt]# sort -n westos ###纯数字排序
1
1
2
2
2
2
4
5
12
43
65
[root@localhost mnt]# sort -nr westos ###纯数字排倒序
65
43
12
5
4
2
2
2
2
1
1
[root@localhost mnt]# sort -nru westos ###纯数字排倒序去冗余
65
43
12
5
4
2
1
[root@localhost mnt]# cat westos
0:1
3:1
2:2
5:2
2:4
7:5
a:2
e:65
v:2
4:12
2:43
[root@localhost mnt]# sort -n -t : -k 2 westos ###第二列按纯数字顺序排列
0:1
3:1
2:2
5:2
a:2
v:2
2:4
7:5
4:12
2:43
e:65
####uniq#####
sort file |uniq -c #####去冗余并统计冗余次数
-d #####显示冗余行
-u #####显示唯一行
[root@localhost mnt]# sort -n westos |uniq -c ###去冗余行并统计次数
2 1
4 2
1 4
1 5
1 12
1 43
1 65
[root@localhost mnt]# sort -n westos |uniq -u ####显示唯一行
4
5
12
43
65
[root@localhost mnt]# sort -n westos |uniq -d ####显示冗余行
1
2
####sed#####
sed ‘s/原字符/替换字符/g‘ file ####替换字符
sed -e ‘策略一‘ -e ‘策略二‘ file ####替换多种字符
sed -i file ####把转换后的内容输入到指定文件
sed ‘3,5s/原字符/替换字符/g‘ ####3-5行替换
sed xd ####屏蔽指定行
sed xp ####复制指定行
sed -n xp ####值显示指定行
[root@localhost mnt]# cat 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
[root@localhost mnt]# sed ‘s/sbin/hello/g‘ passwd ### 替换sbin为hello
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/hello/nologin
daemon:x:2:2:daemon:/hello:/hello/nologin
adm:x:3:4:adm:/var/adm:/hello/nologin
lp:x:4:7:lp:/var/spool/lpd:/hello/nologin
sync:x:5:0:sync:/hello:/bin/sync
[root@localhost mnt]# cat 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
[root@localhost mnt]# sed -e ‘s/sbin/hello/g‘ -e ‘s/nologin/westos/g‘ passwd ####替换sbin为hello,nologin为westos
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/hello/westos
daemon:x:2:2:daemon:/hello:/hello/westos
adm:x:3:4:adm:/var/adm:/hello/westos
lp:x:4:7:lp:/var/spool/lpd:/hello/westos
sync:x:5:0:sync:/hello:/bin/sync
shutdown:x:6:0:shutdown:/hello:/hello/shutdown
[root@localhost mnt]# cat passwd ####(passd文件内容并没改变)
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
[root@localhost mnt]# sed -e ‘s/sbin/hello/g‘ -e ‘s/nologin/westos/g‘ -i passwd #####替换后保存内容到passwd
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/hello/westos
daemon:x:2:2:daemon:/hello:/hello/westos
adm:x:3:4:adm:/var/adm:/hello/westos
lp:x:4:7:lp:/var/spool/lpd:/hello/westos
sync:x:5:0:sync:/hello:/bin/sync
shutdown:x:6:0:shutdown:/hello:/hello/shutdown
######awk#####
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析生成报告时,显得有为强大。
使用方法:awk ‘{pattern + action}‘ {filename}
[root@localhost mnt]# last -n 5
root pts/0 172.25.28.250 Wed Nov 23 05:32 still logged in
root pts/0 :0 Wed Nov 23 05:32 - 05:32 (00:00)
root :0 :0 Wed Nov 23 05:32 still logged in
(unknown :0 :0 Wed Nov 23 05:31 - 05:32 (00:00)
reboot system boot 3.10.0-123.el7.x Wed Nov 23 05:31 - 07:29 (01:57)
wtmp begins Thu Jul 10 18:18:02 2014
[root@localhost mnt]# last -n 5 | awk ‘{print $1}‘ ###值显示最近登陆的5个帐号($1表示第一个域,默认的域分隔符时空格键或tab键)
root
root
root
(unknown
reboot
[root@localhost mnt]# cat /etc/passwd | awk -F ‘:‘ ‘{print $1}‘ ###-F指定分隔符为‘:’
root
bin
daemon
adm
lp
sync
shutdown
halt
[root@localhost mnt]# cat /etc/passwd | awk -F ‘:‘ ‘{print $1 "\t" $7}‘ ###显示/etc/passwd账户及对应的shell,中间用tab键隔开
root/bin/bash
bin/sbin/nologin
daemon/sbin/nologin
adm/sbin/nologin
lp/sbin/nologin
[root@localhost mnt]# cat /etc/passwd | awk -F ‘:‘ ‘BEGIN {print "name shell"} {print $1 "," $7} END {print "blue,bin/nosh"}‘
name shell
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
adm,/sbin/nologin
. . .
student2,/bin/bash
student3,/bin/bash
blue,bin/nosh
[root@localhost ~]# awk -F: ‘/root/‘ /etc/passwd ###搜索/etc/passwd有root关键字的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# awk -F: ‘/^root/‘ /etc/passwd ###搜索开头是关键字root的行
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F: ‘/root$/‘ /etc/passwd
本文出自 “12112684” 博客,请务必保留此出处http://12122684.blog.51cto.com/12112684/1875837
linux 基础 shell脚本命令