0805随堂练习

文本处理练习:

1.找出本机ip地址
  [root@localhost ~]# ifconfig |head -2 |tail -1 |tr -s ' ' ':' |cut -d: -f3
  10.1.252.221

2.查看本机分区最大的利用率
  [root@localhost ~]# df |tr '%' ' ' |tr -s ' ' ':' |cut -d: -f5 |tr '[:alpha:]' ' ' |sort |tail -1
  29

3.查出用户UID最大值的用户名,UID,及shell类型
  [root@localhost ~]# getent passwd |cut -d: -f1,3,7 |sort -t: -k2 -n |tail -1
  nfsnobody:65534:/sbin/nologin

4.查出/tmp的权限,以数字方式显示
  [root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -s ' ' ':' |cut -d: -f2 |tr -cd '[:digit:]'
  1777

5.统计当前连接本机的每个远程主机IP的连接数,并按从小到大排序
  [root@localhost ~]# netstat -nt |grep 'tcp' |tr -s ' ' ':' |cut -d: -f4 |uniq -c
      5 10.1.252.221

正则表达式练习:

1.显示/proc/meminfo文件中以大小写s开头的行

  [root@localhost home]# grep -i '^s' /proc/meminfo
  SwapCached:            0 kB
  SwapTotal:       2098172 kB
  SwapFree:        2098172 kB
  Shmem:              6992 kB
  Slab:              70944 kB
  SReclaimable:      41568 kB
  SUnreclaim:        29376 kB

  [root@localhost home]# grep '^[Ss]' /proc/meminfo
  SwapCached:            0 kB
  SwapTotal:       2098172 kB
  SwapFree:        2098172 kB
  Shmem:              6992 kB
  Slab:              70944 kB
  SReclaimable:      41568 kB
  SUnreclaim:        29376 kB

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

[root@localhost ~]# grep -v '.*/bin/bash$' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
……

3.显示rpc用户默认的shell程序

[root@localhost ~]# grep '^rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

4.找出/etc/passwd中的两位或三位数

[root@localhost ~]# grep -w '[[:digit:]]\{2,3\}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
……

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

[root@localhost ~]# grep '^[[:space:]]\+.*[^[:space:]][^[:space:]]*' /etc/grub2.cfg
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="–id"
  menuentry_id_option=""

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

[root@localhost ~]# netstat -tan |grep '.*LISTEN[[:space:]]*$'
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

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

[root@localhost ~]# getent passwd |grep '^\<\(.*\)\>.*\b\1\b$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1001:1001::/home/bash:/bin/bash
nologin:x:1004:1004::/home/nologin:/sbin/nologin

扩展正则表达式练习:

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

[root@localhost ~]# getent passwd |grep -E '^\<(root|mage|wang)\>' |cut -d: -f3,7
0:/bin/bash
1005:/bin/bash
1006:/bin/bash

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

[root@localhost ~]# grep -E '^(_|[[:alpha:]])+\(' /etc/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {

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

[root@localhost ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '[^/]+/?$'
functions

 4、使用egrep取出上面路径的目录名(待确定)

[root@localhost ~]# echo '/etc/rc.d/init.d/functions' |egrep -o '(/.*/)'
/etc/rc.d/init.d/

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

[root@localhost ~]# last |egrep -o '^root\>.*([[:digit:]]\.)[[:digit:]]+' |tr -s ' ' |cut -d" " -f1,3 |sort |uniq -c
     15 root 10.1.250.107
      1 root 192.168.1.104

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

[0-9]
[1-9][0-9]
1[0-9]{2}
2[0-4][4-9]
25[0-5]

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

[root@localhost ~]# ifconfig |egrep -o '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
10.1.252.221
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0

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

赞 (0)
Mr.LeeMr.Lee
上一篇 2016-08-07 22:07
下一篇 2016-08-07 22:07

相关推荐

  • Linux Bash脚本编程练习

    马哥教育网络班23期+第九周课堂练习 Linux Bash脚本编程练习 练习 1.写一个脚本,判断当前系统上所有用户的shell是否为可登录shell (即用户的shell不是/sbin/nologin) 分别统计这2类用户的个数,通过字符串比较来实现 脚本: 测试结果:  。。。 2.写一个脚本 (1)获取当前主机的主机名 保存于hostname…

    Linux干货 2016-11-28
  • 流式传输的两大主流种类及流式传输特点

     流式传输定义很广泛,现在主要指通过网络传送媒体(如视频、音频)的技术总称。其特定含义为通过Internet 将影视节目传送到PC机。实现流式传输有两种方法:实时流式传输(Realtime streaming)和顺序流式传输(progressive streaming)。(百度百科)     在网络上传输音/视频(英文…

    Linux干货 2015-04-10
  • free / buffer与cache

           前几天看到有些伙伴傻傻分不清楚buffer与cache的用处,后来发现我也不能很清楚的说出来buffer与cache在不同的地方有什么不同之处,这里就总结了一些关于buffer于cache的区别,如有不完善的地方,请大家指出来。        说到buffer与ca…

    2017-07-17
  • LVM基本应用,扩展以及缩减的实现

    这是一篇马哥课堂博客作业,这次换个表现方式写,前面先写总体的操作过程,后面显示详情 其实马哥视频讲的非常详尽,听起来特别容易理解,只是让我要写出来那得费九牛二虎之力,估计还得照着视频写。如下仅是看过视频之后的一个操作过程,比较粗糙,仅仅是作业。。。 LVM:logical volume manager version2 逻辑卷管理系统 PV:physical…

    Linux干货 2016-06-28
  • python之psutil模块

    python 安装psutil 来实现获取系统信息  # yum -y install python*pip # yum -y groupinstall “Development Tools # yum -y install python34-devel.x86_64 # pip3 install –upgrade pip # pip3 inst…

    Linux干货 2017-03-08
  • LVS_DR配置

    DR : 192.168.29.150 VIP:192.168.29.100 RS1: 192.168.29.110 VIP:192.168.29.100 RS2:192.168.29.120 VIP:192.168.29.100 RS 配置 1 配置VIP [root@local ~]# ifconfig lo:0 192.168.29.100 netma…

    2017-06-30