案例
- (1)显示/etc/passwd 文件中以bash结尾的行
[root@bogon ~]# cat /etc/passwd | grep bash$
root:x:0:0:root:/root:/bin/bash
- (2)找出/etc/passwd文件中的三位数或四位数
[root@bogon ~]# grep '[0-9]\{3,4\}' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:997:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockpit-ws:x:996:993:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:995:992:User for cockpit-ws instances:/nonexisting:/sbin/nologin
sssd:x:994:990:User for sssd:/:/sbin/nologin
chrony:x:993:989::/var/lib/chrony:/sbin/nologin
rngd:x:992:988:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
nginx:x:991:987:Nginx web server:/var/lib/nginx:/sbin/nologin
chen:x:1000:1000::/home/chen:/bin/bash
[root@bogon ~]# grep ':[0-9]\{3,4\}:' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:997:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
cockpit-ws:x:996:993:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:995:992:User for cockpit-ws instances:/nonexisting:/sbin/nologin
sssd:x:994:990:User for sssd:/:/sbin/nologin
chrony:x:993:989::/var/lib/chrony:/sbin/nologin
rngd:x:992:988:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
nginx:x:991:987:Nginx web server:/var/lib/nginx:/sbin/nologin
chen:x:1000:1000::/home/chen:/bin/bash
- (3)找出chen1文件中,以至少一个空白字符开头,后面又跟了非空白字符的行
[root@bogon ~]# cat chen1
a
11111
1111
111
1
111
[root@bogon ~]# egrep '^ +.*' chen1
11111
1111
111
111
- (4)找出"netstat -tan"命令的结果中,以“LISTEN”后跟0个或多个以空白字符结尾的行
[root@bogon ~]# netstat -tan | grep 'LISTEN.* \{0,\}$'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
- (5)找出系统中用户的用户名与shell名相同的用户
[root@bogon ~]# grep '^\(.*\):.*\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
- (6)找出"fdisk -l"命令的结果中,包含/dev/nvme0n1p1和/dev/nvme0n1p2
[root@bogon ~]# fdisk -l | egrep '/dev/nvme0n1p(1|2)'
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 41943039 39843840 19G 8e Linux LVM
- (7)找出"ldd /usr/bin/cat"命令的结果中的文件路径
[root@bogon ~]# ldd /usr/bin/cat
linux-vdso.so.1 (0x00007ffcee1c2000)
libc.so.6 => /lib64/libc.so.6 (0x00007fe88fee3000)
/lib64/ld-linux-x86-64.so.2 (0x00007fe8904ae000)
[root@bogon ~]# ldd /usr/bin/cat | grep /lib64/ | awk -F" " '{print $(NF-1)}'
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
- (8)找出/proc/meminfo文件中,所有以大写或小写s开头的行
[root@bogon ~]# cat /proc/meminfo | grep ^[sS].*
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 8876 kB
Slab: 104716 kB
SReclaimable: 39532 kB
SUnreclaim: 65184 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
- (9)找出ifconfig命令结果中1-255之间的整数
[root@bogon ~]# ifconfig | grep "inet " | tr -s " " | cut -f3 -d" "
192.168.150.136
127.0.0.1
- (10)显示当前系统上root,nginx或者chen用户的相关信息
[root@bogon ~]# cat /etc/passwd | egrep '(root|nginx|chen)'
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
nginx:x:991:987:Nginx web server:/var/lib/nginx:/sbin/nologin
chen:x:1000:1000::/home/chen:/bin/bash