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 10:27
下一篇 2016-08-10 10:27

相关推荐

  • 常用的MySQL数据库备份

    常用的MySQL数据库备份方式 前言 为什么需要备份数据? 数据的备份类型 MySQL备份数据的方式 备份需要考虑的问题 设计合适的备份策略 实战演练 使用cp进行备份 使用mysqldump+复制BINARY LOG备份 使用lvm2快照备份数据 使用Xtrabackup备份 总结 前言 我们试着想一想, 在生产环境中什么最重要?如果我们服务器的硬件坏了可…

    2015-06-13
  • 数据库存储引擎—InnoDB

    MySQL中的数据用各种不同的技术存储在文件(或者内存)中。这些技术中的每一种技术都使用不同的存储机制、索引技巧、锁定水平并且最终提供广泛的不同的功能和能力。通过选择不同的技术,你能够获得额外的速度或者功能,从而改善你的应用的整体功能。

    Linux干货 2018-02-22
  • 正则表达式

    正则表达式课程笔记

    2017-11-05
  • CentOS系统安装

    centos系统安装        安装程序:anaconda,Anaconda是RedHat、CentOS、Fedora等Linux的安装管理程序。它可以提供文本、图形等安装管理方式,并支持Kickstart等脚本提供自动安装的功能。此外,其还支持许多启动参数,熟悉这些参数可为安装带来很多方便。该程序的功能是把位于光…

    Linux干货 2016-09-23
  • 文本处理三剑客-sed

    1.文本处理三剑客-sed #sed语法: sed  [OPTION]…  {script-only-if-no-other-script}        [input-file]… sed理解:是一种流编辑器,一次处理一行内容,处理时把当前…

    Linux干货 2016-08-12
  • Linux基础学习总结(六)

    一、创建一个10G分区,并格式为ext4文件系统; 1、要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; [root@blog ~]# fdisk /dev/sdfCommand (m for help): nCommand action e extended p primary partition (1…

    2016-11-06