面授20期2班-08月4号课堂与课后习题

课堂习题

1、显示/etc/init.d/functions文件中所有的单词及出现的次数

cat /etc/init.d/functions | tr -sc "[:alpha:]" '\n' |sort | uniq -c

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

centos6:ifconfig | head -2 | tail -1 | cut -d: -f 2 | cut -d" " -f1

centos7:ifconfig | head -2 | tail -1 | cut -dt -f2 | cut -d" " -f2

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

[root@shao ~]# df | cut -c 44-46 | sort -nr | head -n2 | tail -n1

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

方法一 cat /etc/passwd | cut -d: -f1,3,7 | sort -nrt : -k 2 |head -n 1

方法二 sort -nrt: -k3 /etc/passwd | head -n1 | cut -d: -f1,3,7

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

 stat /tmp | head -4 | tail -1 | cut -d/ -f1 | cut -d\( -f2

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

netstat -tn | cut -d: -f2 | tr -s " " ":" | cut -d: -f2 | sort | uniq -c

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

方法一 grep -e ^s -e ^S  /proc/meminfo
方法二 grep -i ^s  /proc/meminfo
方法三 grep  ^[sS]  /proc/meminfo

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

grep -v "/bin/bash$" /etc/passwd

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

getent passwd | grep ^"\<rpc\>" | cut -d: -f1,7

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

grep -o "\<[[:digit:]]\{2,3\}\>" /etc/passwd

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

grep -o  "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cf

12、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、1 或多个空白字符结尾的行

netstat -tan | grep "\<LISTEN[[:space:]]*$"

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

 useradd -s /sbin/nologin nologin
 useradd bash
 useradd testbash

方法一 getent passwd | grep "^\(\<.*\>\).*\<\1\>$"

方法二 getent passwd | grep "^\(\<[[:alnum:]]\{1,\}\>\).*\<\1\>$"

14、显示当前系统root或mage用户的UID和默认shell

方法一 grep -E "^(root|mage):" /etc/passwd | cut -d: -f3,7

方法二 grep -e "^root\<" -e "^mage\<" /etc/passwd | cut -d: -f3,7

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

grep  -E  "^([[::alpha]_])+\(\).*" /etc/rc.d/init.d/functions

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

echo /etc/rc.d/init.d/functions | egrep -o "f.*$"

17、使用egrep取出上面路径的目录名

echo /etc/rc.d/init.d/functions | egrep -o "^.*/"

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

last | grep root | egrep -o "[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+" | uniq -c | sort -t" "


课后练习

1、用扩展正则表达式表示IP地址

ifconfig | egrep -o "\<(((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\>"

2、用扩展正则表达式表示手机号13 14 17 15  18

egrep -o "\<1[134578][0-9]{9}\>"

3、用扩展正则表达式表示身份证号18

egrep -o "\((1[1-5]) | (2[1-3]) | (3[1-7]) | (4[1-6]) | (5[0-4]) | (6[1-5]) | (71、81\82))([0-9]){4}(19|20)([0-9]){2}((0[1-9]) | (1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>"

4、用扩展正则表达式表示邮箱

qq邮箱:

egrep -o "\<[1-9][0-9]{4,9}@qq.com\>"

所有邮箱 :

egrep -o "\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]\.)+[[:alnum:]]+"

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

(0)
liushaoshaoliushaoshao
上一篇 2016-08-08
下一篇 2016-08-08

相关推荐

  • 第三周作业

    1、列出1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。[root@localhost ~]# who | cut -d ‘ ‘ -f1 | sort -u 2、取出最后登录到当前系统的用户的相关信息。[root@localhost ~]# who | tail -1 | export &am…

    Linux干货 2017-12-16
  • 关于源码包的基本知识

    关于源码包的基本知识  §·什么是程序 程序(Program)是为实现特定目标或解决特定问题而用计算机语言编写的命令序列的集合。为实现预期目的而进行操作的一系列语句和指令。 一般分为系统程序和应用程序两大类。 程序就是为使电子计算机执行一个或多个操作,或执行某一任务,按序设计的计算机指令的集合。 §·程序包的编译安装 ※·为什么需要源码安装 1.最…

    Linux干货 2016-08-24
  • 1017作业

    1 生产环境发现一台服务器系统时间产生偏差,造成服务异常,请帮忙校正 ##先分析硬件时间不对还是系统时间不对,如果是系统时间不对: [root@localhost ~]# hwclock -w [root@localhost ~]#  ##如果是硬件时间不对: [root@localhost ~]#…

    Linux干货 2016-10-18
  • N25第一周作业

    个人习惯用导图写的作业

    Linux干货 2016-12-04
  • Linux bash中命令执行状态返回值

    Linux bash中命令执行状态返回值 在操作系统中,命令的执行后输出的内容为命令执行结果输出,而这个命令本身是否执行成功,它是通过命令执行状态返回值来标识的。 常用的值: 0 表示命令执行成功非0 表示命令执行失败 bash中获取命令执行状态返回值的方法 在刚执行完一条指令后,使用echo $?取得上一条指令的命令执行状态返回值,示例如下:  …

    Linux干货 2016-11-06
  • 从Linux小白到大牛——与狼共舞的日子13

    马哥教育网络班21期+第13周课程练习 1、建立samba共享,共享目录为/data,要求:(描述完整的过程) 1)共享名为shared,工作组为magedu; 2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名; 3)添加sa…

    Linux干货 2017-01-03