8.5_Linux习题练习和作业

课堂练习题

1、找出ifconfig命令结果中本机的所有IPv4地址

# ifconfig | grep -oE "([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}"

blob.png

2、查出分区空间使用率的最大百分比值

# df |grep "/dev/sd" | tr -s " " ":" |cut -d: -f1,5 |sort

blob.png

3、查出用户UID最大值的用户名、UID及shell类型

# cat /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1,3,7

blob.png

4、查出/tmp的权限,以数字方式显示

# stat /tmp |grep 'Access' | head -1 | tr " " ":" | cut -d: -f3 |tr -cd "[[:digit:]]"

blob.png

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos7 ~]# netstat -nt | grep "tcp"|tr -s " " ":"|cut -d: -f6 |sort |uniq -c

blob.png

6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)

(1)grep "^[sS]" /proc/meminfo

blob.png

(2)grep -i "^s" /proc/meminfo

blob.png

7、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@centos7 ~]# grep -v "/bin/bash$" /etc/passwd

8、显示用户rpc默认的shell程序

grep "^rpc\>" /etc/passwd | cut -d: -f1,7

blob.png

9、找出/etc/passwd中的两位或三位数

[root@centos7 ~]# grep -E "\b([1-9][0-9]|[1-9][0-9]{2})\b" /etc/passwd

10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@centos7 ~]# grep "^\s[^\s].*" /etc/grub2.cfg

11、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行

[root@centos7 ~]# netstat -tan | grep "LISTEN\s*$"

blob.png

12、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@centos7 ~]# grep -E "(^[[:alnum:]]+)\b.*/\1{1}$" /etc/passwd

blob.png

13、显示三个用户root、mage、wang的UID和默认shell

[root@centos7 ~]# grep -E "^(root|mage|wang)\>" /etc/passwd | cut -d: -f1,3,7

blob.png

14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos7 ~]# egrep "\w+\(\).*" /etc/rc.d/init.d/functions

blob.png

15、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep "\w*\.?\w*$"

blob.png

16、统计以root身份登录的每个远程主机IP地址的登录次数

[root@centos7 ~]# last | grep "root" | egrep -o "([0-9]|[0-9]{2}|[0-9]{3})(\.([0-9]|[0-9]{2}|[0-9]{3})){3}"|sort|uniq -c

blob.png

17、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

18、显示ifconfig命令结果中所有IPv4地址

[root@centos7 ~]# ifconfig | egrep -o "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}"

blob.png

作业题

1、取本机ip地址

[root@centos7 ~]# ifconfig|head -2|tail -1|grep -oE "([0-9]|[1-9][0-9]|[1-9][0-9]{2})(\.([0-9]|[1-9][0-9]|[1-9][0-9]{2})){3}"|head -1

blob.png

2、取各分区利用率的数值

[root@centos7 ~]# df -h | grep "/dev/sd" | tr -s " " ":" | cut -d: -f1,5 | sort

blob.png3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@centos7 ~]# grep -o "\b[[:alnum:]]*\b" /etc/init.d/functions | sort | uniq -c | sort -n

blob.png

4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "/.*/"

blob.png

5、正则表达式表示身份证号

egrep "\b[1-9]{2}[0-9]{15}[0-9xX]\b"

6、正则表达式表示手机号

egrep "\b1[3-8][0-9]{9}\b"

7、正则表达式表示邮箱

[root@centos7 ~]# egrep -i "\w*@[[:alnum:]]*\.[[:alpha:]]{1,3}\.?[[:alpha:]]{,2}"

8、正则表达式表示QQ号

 [root@centos7 ~]# egrep "\b[1-9][0-9]{4,9}\b"

原创文章,作者:~微风~,如若转载,请注明出处:http://www.178linux.com/30832

(0)
~微风~~微风~
上一篇 2016-08-10
下一篇 2016-08-10

相关推荐

  • ​HA专题: Corosync+Pacemaker+drbd实现MySQL高可用

    HA专题: Corosync+Pacemaker+drbd实现MySQL高可用 前言 实验拓扑 实验环境 实验步骤 安装前准备工作 配置DRBD 配置MySQL 配置Corosync+Pacemaker 测试 我遇到的问题 总结 前言 上篇文章我们介绍了drbd的相关原理、编译安装、简单的实现. drbd虽然可以保证数据的可靠性但是我们上次的实现还…

    Linux干货 2016-04-13
  • httpd-2.4 功能生动实现 (Blog 13)

    CentOS 7 与 CentOS 6 访问控制区别、虚拟主机

    2017-12-01
  • 关于大型网站技术演进的思考(二):存储的瓶颈(2)

    原文出处: 夏天的森林   上篇里我讲到某些网站在高并发下会报出503错误,503错误的含义是指网站服务端暂时无法提供服务的含义,503还表达了网站服务端现在有问题但是以后可能会提供正常的服务,对http协议熟悉的人都知道,5开头的响应码表达了服务端出现了问题,在我们开发测试时候最为常见的是500错误,500代表的含义是服务端程…

    2015-03-11
  • Linux磁盘及文件系统管理

    Linux磁盘及文件系统管理 创建文件系统      mkfs.ext#     .xfs mke2fs 功能比mkfs强大的多 检测及修复     fsck      …

    Linux干货 2016-08-30
  • Linux 第二天: (07月22日) 练习和作业

    Linux 第二天: (07月22日) 练习和作业         echo 显示闪烁, 下划线, 彩色, 倒三角形 ******* *****  ***   *   #!/bin/bash read -p "Input line number: "…

    Linux干货 2016-08-08
  • Linux哲学思想

    一切皆文件(硬件设备亦如此); 由众多目的单一的小程序组成,一个程序只做一件事,并且要好; 组合小程序完成复杂任务; 尽可能避免跟用户交互; 使用文本文件保存配置信息; 提供机制,而非策略。 Linux作为自由软件(GNU),同时也遵循自由软件的思想: 自由使用 自由学习和修改 自由分发 自由创建衍生版

    Linux干货 2016-10-31