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

相关推荐

  • MBR&GPT分区基本原理

    1、磁盘系统为什么要分区?     •     优化I/O性能     •     实现磁盘空间配额限制       &…

    Linux干货 2016-08-30
  • 文件通配符是啥?

    文件通配符     包含 ‘?’, ‘*’ 或 ‘[‘ 字符的字符串称为通配符模板(wildcard  pattern).形成路径名(globbing)指一种操作,把通配符模板展开为匹配该串的路径名.匹配定义为:   …

    Linux干货 2017-04-23
  • 第二周学习总结

    写在最前 通过上一周对计算机系统、Linux发展的学习,我们认识到了Linux是一个一切皆文件的内核系统。那么怎样去操作系统上的各文件来达到你想要的目的呢?今天的总结就是帮助我们更快的达成目标。 文件管理类命令的分类 对于这类命令主要分为两类: 操作类:touch、cp、mv 查看类:cat、tac、head、tail、less、more touch 功能描…

    2017-12-16
  • Linux软件包安装—-卷一软件包管理rpm方式安装程序

    rpm方式手动安装程序,可能需要”人工智能”的方式手动去处理程序依赖问题

    Linux干货 2017-12-02
  • Linux文件权限及ACL

    文件权限 文件或目录可存取的身份分为3类,owner/group/others (所属者/所属组/其他人) ,所以访问一个文件或目录的权限分为三种, 所有者权限,所属组权限,其他人权限 (通过ll text.log查看)。 在linux系统中常用的权限分为3种 r(read) w(write) x(excute)。 对文件或目录的权限进行修改时,要用到chm…

    Linux干货 2017-06-01
  • 第三周(3):课堂练习与作业

    课堂练习: 1、找出ifconfig命令结果中本机的所有IPv4地址 [root@centos6 ~]# ifconfig | tr -cs '[0-9].' '\n'|sort -ut. -k3n 127.0.0.1 10.1.…

    Linux干货 2016-08-08