shell脚本常用基础命令2
shell脚本常用基础命令
- shell脚本常用基础命令2
- 一、grep用法
- 二、sed用法
- 2.1p参数 (显示)n参数(只显示处理过的行)
文本处理三剑客:grep sed awk
一、grep用法
grep -E = egrep (扩展搜索正文表达式)
grep格式
grep 匹配条件 处理文件(很多正文表达式用不了) |表示或者
[root@docker3 mnt]# grep "bash|root" passwd
[root@docker3 mnt]# egrep "bash|root" passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -E "bash|root" passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -e bash -e root passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
grep开启的是贪婪模式,要是不匹配前面的<,不匹配后面>
[root@docker3 mnt]# grep -E "root" passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
chroot
rootch
[root@docker3 mnt]# grep -E "\<root" passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -E "\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
grep 前面加数字,表示该关键字前面后面几行都显示出来
[root@docker3 mnt]# grep chroot passwd
chroot
[root@docker3 mnt]# grep -2 chroot passwd
test:root:test
TEST:ROOT:TEST
chroot
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -A2 chroot passwd
chroot
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -B2 chroot passwd
test:root:test
TEST:ROOT:TEST
chroot
显示匹配的在第几行
[root@docker3 mnt]# grep -n chroot passwd
12:chroot
反向过滤,除了chroot其它行都过滤出来,展示部分。
[root@docker3 mnt]# grep -v chroot passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
以^…开头,…¥以…结尾
[root@docker3 mnt]# grep -E "^root" passwd
root:x:0:0:root:/root:/bin/bash
rootch
[root@docker3 mnt]# grep -E "bash$" passwd
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -E "ws" yan
ws
[root@docker3 mnt]# grep -E "w.s" yan
was
[root@docker3 mnt]# grep -E "w...s" yan
waaas
[root@docker3 mnt]# grep -E "w.*s" yan
ws
was
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "w.?s" yan
ws
was
[root@docker3 mnt]# grep -E "w.+s" yan
was
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "w.{3}s" yan
waaas
[root@docker3 mnt]# grep -E "w.{2,3}s" yan
waaas
[root@docker3 mnt]# grep -E "w.{,3}s" yan
ws
was
waaas
[root@docker3 mnt]# grep -E "w.{3,}s" yan
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "wab{1,}s" yan
[root@docker3 mnt]# grep -E "w(ab){1,}s" yan
wabababs
脚本
请显示系统中能被su命令切换的用户名称
[root@docker3 mnt]# sh user_check.sh
root
yan
二、sed用法
用来对文本的内容进行处理,修改的是模式空间(内存),真实的(磁盘中)
sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件
对字符的处理
p 显示
d 删除
a 添加
n 取消默认输出,sed默认会输出所有文本内容,使用-n参数后只显示处理过的行
i 插入,直接修改文件内容
c 替换
w 写入
r 整合文件
2.1p参数 (显示)n参数(只显示处理过的行)
[root@docker3 mnt]# sed -n 5p passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@docker3 mnt]# vim rule
[root@docker3 mnt]# cat rule
5p
[root@docker3 mnt]# sed -n -f rule passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@docker3 mnt]# sed '5p' fstab
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed -n '5p' fstab
#5
[root@docker3 mnt]# sed -n '3,5p' fstab
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
[root@docker3 mnt]# sed -n '3p;5p' fstab
# /etc/fstab3
#5
#开头的行以及不是#开头的行
[root@docker3 mnt]# sed -n '/^#/p' fstab
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
[root@docker3 mnt]# sed -n '/^#/!p' fstab
1
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
^$空行
[root@docker3 mnt]# sed -n '/^#/!p' fstab | sed -n '/^$/!p'
1
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
最后一行$p
[root@docker3 mnt]# sed -n '$p' fstab
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed -n '5,$p' fstab
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
显示行号,‘=’, '$='最后一行的行号(可以统计文件有多少行)
[root@docker3 mnt]# sed -n '$=' fstab
11
[root@docker3 mnt]# sed -n -e '3p' -e '5p' fstab
# /etc/fstab3
#5
d参数删除
[root@docker3 mnt]# sed 5d fstab
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed '3,5d' fstab
1
#2
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed '3d;5d' fstab
1
#2
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed '/^#/d' fstab
1
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
a参数添加
[root@docker3 mnt]# sed '/^#/ahello' fstab
1
#2
hello
# /etc/fstab3
hello
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
hello
#5
hello
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
hello
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
hello
#8
hello
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
最后一行后面添加
[root@docker3 mnt]# sed '$ahello' fstab
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
hello
[root@docker3 mnt]# sed '$ahello\nyan' fstab
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
hello
yan
i参数插入 i是在最后一行前面,a是添加到最后一行
[root@docker3 mnt]# sed '$ihello\nyan' fstab
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
hello
yan
/dev/mapper/rhel-swap swap swap defaults 0 011
[root@docker3 mnt]# sed '1ihello\nyan' fstab
hello
yan
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
c参数替换
[root@docker3 mnt]# sed '/^#/chello' fstab
1
hello
hello
hello
hello
hello
hello
hello
/dev/mapper/rhel-root / xfs defaults 0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot xfs defaults 0 010
/dev/mapper/rhel-swap swap swap defaults 0 011
w参数写入
[root@docker3 mnt]# sed '/^#/w file ' fstab
[root@docker3 mnt]# cat file\
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
就是
[root@docker3 mnt]# sed -n '/^#/p' fstab > file
[root@docker3 mnt]# cat file
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
r
[root@docker3 mnt]# cat yan
1
2
3
[root@docker3 mnt]# cat yan1
4
5
6
[root@docker3 mnt]# cat yan yan1
1
2
3
4
5
6
[root@docker3 mnt]# cat yan yan1 > file
倒叙
[root@docker3 mnt]# tac yan
3
2
1
[root@docker3 mnt]# tac yan yan1
3
2
1
6
5
4
[root@docker3 mnt]# tac yan1 yan
6
5
4
3
2
1
cat tac无法把文件yan1整合到数字1后面,可以正序也可以倒序
1r第一行后面
[root@docker3 mnt]# sed '1ryan1' yan
1
4
5
6
2
3
整合到某关键字后面
[root@docker3 mnt]# sed '/2/ryan1' yan
1
2
4
5
6
3
更改到原文件
[root@docker3 mnt]# sed '/2/ryan1' -i yan
[root@docker3 mnt]# cat yan
1
2
4
5
6
3
脚本
Apache_port.sh
此脚本接入数字
http的端口就改为此数字
假设selinux为关闭状态
[root@docker3 mnt]# sh Apacha_port.sh
Error: Please input port number following script!!!
[root@docker3 mnt]# sh Apacha_port.sh 80
Error:Apache is not installed!!
双引号不能被引用变量,单引号会注释变量,所以Listen注意是要双引号
[root@docker3 mnt]# sh Apacha_port.sh 8080
[root@docker3 mnt]# netstat -antlp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 4140/httpd
[root@docker3 mnt]# sh Apacha_port.sh 80
[root@docker3 mnt]# netstat -antlp | grep 80
tcp6 0 0 :::80 :::* LISTEN 4172/httpd
sed字符替换
s是全文的行,g全文的列
部分展示 /表示分割符
[root@docker3 mnt]# sed 's/sbin/hello/g' passwd
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
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/hello/nologin
postfix:x:89:89::/var/spool/postfix:/hello/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/hello/nologin
yan:x:1000:1000::/home/yan:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/hello/nologin
[root@docker3 mnt]# sed 's/:/ /g' 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
[root@docker3 mnt]# sed 's/:/ /' 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@docker3 mnt]# sed '1,5s/:/ /' 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
[root@docker3 mnt]# sed '/lp/,/halt/s/:/ /g' 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
最后一行
[root@docker3 mnt]# sed '$s/:/ /g' passwd
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
apache x 48 48 Apache /usr/share/httpd /sbin/nologin
[root@docker3 mnt]# sed '$s/:/ /g;s/sbin/hello/g' passwd
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
shutdown:x:6:0:shutdown:/hello:/hello/shutdown
替换/,/需要转义,因为它是分割符
[root@docker3 mnt]# sed 's/:/ /g;s/\//##/g' 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
/表示分割符,可以换成@
[root@docker3 mnt]# sed 's@:@ @g;s@/@##@g' 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
END