23、grep/egrep正则表达式练习:
1) 显示/etc/passwd文件中不以bash结尾的行;
答:
# grep -v "bash$" /etc/passwd
2) 找出ifconfig命令执行结果中的两位或三位整数;
答:
# ifconfig | grep --color=auto "\<\([[:digit:]]\{2,3\}\)\>" //还有00
# ifconfig | grep --color=auto "\<\([1-9][0-9]\|[1-9][0-9][0-9]\)\>" //完美
3) 找出/etc/grub2.cfg(在CentOS6中是/etc/grub.conf)文件中,以空白字符开头的非空白行;
答:
# cat /etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$" //centos6
# grep "^[[:space]]\+[^[:space:]]\+" /etc/grub2.cfg //centos7
default=0
设定默认启动菜单项;整个菜单中的所有的title是从0开始编号的;
timeout=5
用户未作出选择时,grub会自动引导默认菜单对应的内核的超时时间;
splashimage=(hd0,0)/grub/splash.xpm.gz
grub的背景图片对应的路径;
hiddenmenu
隐藏grub的启动菜单
password --md5 CRYPTED_PASSWORD
为了保护菜单设置的密码,通常用于防止随意进入单用户模式;
title CentOS 6 (2.6.32-573.el6.x86_64)
定义菜单项中的各个"标题",可以定义多个title项;至少有一项;
root (hd0,0)
指定grub的根设备,通常是用来安装grub的那个分区;
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=5ebb0e76-bb19-4a80-9c70-0d101c0778e1 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
该菜单对应的要启动的内核及传递给内核的各个参数;
initrd /initramfs-2.6.32-573.el6.x86_64.img
内核所对应的ramdisk(ramfs)文件
password --md5 CRYPTED_PASSWORD
保护操作系统内核的启动;
4) 找出"netstat -tan”命令的结果中,以"LISTEN"后跟任意个空白字符结尾的行;
答:
# netstat -tan | grep ".*LISTEN[[:space:]]*$"
# netstat -tan | grep "LISTEN[[:space:]]*$"
5) 找出"fdisk -l"命令的结果中,包含以/dev/后跟sd或hd及一个小写字母的行;
答:
# fdisk -l | grep "^/dev/[s|h]d[a-z]"
6) 找出"ldd /usr/bin/vim"命令的结果中所有的文件路径;
答:
# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+"
# ldd /usr/bin/vim | grep -o "/.*/.*"
7) 找出/proc/meminfo文件中,所有以大写S或小写s开头的行,你有多少种方法实现该任务?
答:
# cat /proc//meminfo | grep "^[Ss].*" //内存使用情况
# cat /proc//meminfo | grep -E "^(S|s).*"
# cat /proc//meminfo | grep -i "^s" //忽略大小写
# cat /proc/meminfo | grep "^s\|^S" //注意解释输出“或”
# cat /proc/meminfo | grep -e "^s" -e "^S" //多项匹配
8) 使用egrep取出某个路径的基名;
答:
# ls /usr/bin/ldd | grep -o "/[a-z]\+$"
9) 显示出ifconfig命令结果中的0-255之间的整数;
答:
# ifconfig | grep --color=auto "\<\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\)\>"
10) 添加用户bash, testbash, basher, nologin, unlogin, logining,要求前三个用户的默认shell为/bin/bash,后三个用户的默认shell为/sbin/nologin,而后找出其用户名与shell名相同的用户信息;
答:
# useradd -s /bin/bash bash
# useradd -s /bin/bash basher
# useradd -s /bin/bash testbash
# useradd -s /sbin/nologin nologin
# useradd -s /sbin/nologin unlogin
# useradd -s /sbin/nologin logining //创建多用户可用脚本来完成
# cat /etc/passwd | grep "^\<\([[:alnum:]]\+\)\>.*\1$"
例:
[root@little ~]# grep -v "bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
……
[root@little ~]# ifconfig | grep --color=auto "\<\([[:digit:]]\{2,3\}\)\>"
eth0 Link encap:Ethernet HWaddr 00:0C:29:EF:91:5E
inet addr:172.16.67.2 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:feef:915e/64 Scope:Link
RX bytes:638912 (623.9 KiB) TX bytes:587812 (574.0 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:EF:91:68
inet addr:192.168.222.136 Bcast:192.168.222.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:9168/64 Scope:Link
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:112446 (109.8 KiB) TX bytes:1334 (1.3 KiB)
eth2 Link encap:Ethernet HWaddr 00:0C:29:EF:91:72
inet addr:192.168.222.137 Bcast:192.168.222.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:9172/64 Scope:Link
TX packets:175 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:120635 (117.8 KiB) TX bytes:17648 (17.2 KiB)
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
RX packets:46 errors:0 dropped:0 overruns:0 frame:0
TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
[root@little ~]#
[root@little ~]# cat /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda1
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-573.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-573.el6.x86_64.img
[root@little ~]#
[root@little ~]# cat /etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$"
root (hd0,0)
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-573.el6.x86_64.img
[root@little ~]#
[root@little ~]# netstat -tan | grep ".*LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:36239 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 :::111 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::33813 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 ::1:6010 :::* LISTEN
[root@little ~]#
[root@little ~]# fdisk -l | grep "^/dev/[s|h]d[a-z]"
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 6553 52428800 83 Linux
/dev/sda3 6553 6814 2097152 82 Linux swap / Solaris
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/.*/.*"
/lib64/libm.so.6 (0x0000003634800000)
/lib64/libselinux.so.1 (0x0000003635400000)
/lib64/libtinfo.so.5 (0x000000363d000000)
/lib64/libacl.so.1 (0x0000003c76800000)
/usr/lib64/libgpm.so.2 (0x0000003634c00000)
/usr/lib64/perl5/CORE/libperl.so (0x0000003634400000)
/lib64/libdl.so.2 (0x0000003633800000)
/lib64/libpthread.so.0 (0x0000003634000000)
/lib64/libc.so.6 (0x0000003633c00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003633400000)
/lib64/libattr.so.1 (0x0000003643800000)
/lib64/libresolv.so.2 (0x0000003635800000)
/lib64/libnsl.so.1 (0x0000003644c00000)
/lib64/libcrypt.so.1 (0x000000363f800000)
/lib64/libutil.so.1 (0x0000003643c00000)
/lib64/libfreebl3.so (0x000000363f400000)
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+"
/lib64/libm.so.6
/lib64/libselinux.so.1
/lib64/libtinfo.so.5
/lib64/libacl.so.1
/usr/lib64/libgpm.so.2
/usr/lib64/perl5/CORE/libperl.so
/lib64/libdl.so.2
/lib64/libpthread.so.0
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
/lib64/libattr.so.1
/lib64/libresolv.so.2
/lib64/libnsl.so.1
/lib64/libcrypt.so.1
/lib64/libutil.so.1
/lib64/libfreebl3.so
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+" | wc -l
16
[root@little ~]# ldd /usr/bin/vim | grep -o "/.*/.*" | wc -l
16
[root@little ~]# cat /proc//meminfo | grep "^[S|s].*"
SwapCached: 48 kB
SwapTotal: 2097148 kB
SwapFree: 2094960 kB
Shmem: 3136 kB
Slab: 85056 kB
SReclaimable: 15576 kB
SUnreclaim: 69480 kB
[root@little ~]# ls /usr/bin/ldd | grep -o "/[a-z]\+$"
/ldd
[root@little ~]#
[root@little ~]# ifconfig | grep --color=auto "\<\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\)\>"
[root@little ~]#
[root@little ~]# cat /etc/passwd | grep "^\<\([[:alnum:]]\+\)\>.*\1$"
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@little ~]#
24.脚本练习:
1) 编写一个脚本,要求:
统计/etc, /var, /usr目录中所有非隐藏的一级子目录和文件的数量总和;
答:
#!/bin/bash
#
a=$(ls /etc | wc -l)
b=$(ls /var | wc -l)
c=$(ls /usr | wc -l)
count=$[$a+$b+$c]
echo "$count"
[root@little ~]# vi 24p1.sh
[root@little ~]#
[root@little ~]# chmod +x 24p1.sh
[root@little ~]#
[root@little ~]# ./24p1.sh
303
2) 编写一个脚本,要求:
对于目录/tmp/testdir1,如果当前有效用户有读、写和执行的权限,那么就在该目录中创建一个文件;否则,显示“没有权限不能创建文件”的信息。
答:
#!/bin/bash
#
FPATH=/tmp/testdir1
[ -r $FPATH -a -w $FPATH -a -x $FPATH ] && touch $FPATH/newdir || echo "Don't right to create useradd"
[root@little ~]# vi 24p2.sh
[root@little ~]#
[root@little ~]# ll /tmp/testdir1
total 0
[root@little ~]# chmod +x 24p
24p1.sh 24p2.sh
[root@little ~]# chmod +x 24p2.sh
[root@little ~]#
[root@little ~]# ./24p2.sh
[root@little ~]#
[root@little ~]# ll /tmp/testdir1
total 0
-rw-r--r--. 1 root root 0 Nov 21 11:29 newdir
[root@little ~]#
3) 编写一个脚本,要求:
从/etc/passwd中UID和GID相同的用户中随机选择一个用户,判断其用户名和基本组名是否相同;
答:
#!/bin/bash
#
username=$(grep "\<\([[:digit:]]\+\).*\1\>" /etc/passwd | sort -R | head -1 | cut -d":" -f1)
groupname=$(id -gn $username)
[ $username == $groupname ] && echo "username and group is same." || "username and groupname is different."
[root@little ~]# vi 24p3.sh
[root@little ~]#
[root@little ~]# chmod +x 24p3.sh
[root@little ~]#
[root@little ~]# ./24p3.sh
cut: invalid option -- '1'
Try `cut --help' for more information.
./24p3.sh: line 5: [: ==: unary operator expected
./24p3.sh: line 5: username and groupname is different.: command not found
[root@little ~]#
[root@little ~]# vi +5 24p3.sh
[root@little ~]#
[root@little ~]# ./24p3.sh
username and group is same.
[root@little ~]#