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)
上一篇 2016-08-07 22:07
下一篇 2016-08-07 22:07

相关推荐

  • Linux系统管理常用命令

    系统管理工具 进程的分类: CPU-Bound:CPU密集型,非交互。特别消耗CPU的,加密解密,压缩解压 IO-Bound:IO密集型,交互。大量的硬盘读写,例如复制文件 Linux系统状态的查看及管理工具:pstree, ps, pidof, pgrep, top, htop, glance, pmap, vmstat, dstat, kill, pki…

    Linux干货 2017-12-18
  • 文本处理-vim编辑器

    命令格式、(1)模式、(2)关闭文件、(3)可视化模式、(4)使用多个“窗口”、(5)定制vim的工作特性、(6)示例

    2018-03-13
  • linux基础学习之SElinux

    1、SElinux简介 SELinux: Secure Enhanced Linux,是美国国家安全局「NSA=The National Security Agency」和SCC(Secure Computing Corporation)开发的Linux的一个强制访问控制的安全模块。2000年以GNU GPL发布,Linux内核2.6版本后集成在内核中 2、…

    Linux干货 2016-09-15
  • 文件查找——藏的在深也没用

    locate 依赖与事先构建好的数据库查找          系统自动实现(周期性任务)          手动更新数据库(updatedb) 工作特性    …

    Linux干货 2016-08-15
  • 推荐-LAMP的编译安装

    首先解释一下LAMP,L:Linux;A:apache;M:MariaDB;P:php。Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件。 本文就是介绍编译安装apache2.4,MariaDB5.5,以及php(基于模块化和fpm的这两种方式来配合php提供服务)。 因为php是…

    Linux干货 2016-04-11
  • Linux文件管理相关及命令别名、执行状态等

    Linux文件管理相关命令有:cp,mv,rm         文件查看类命令有:cat,tac,head,tail,more,less         接下来,逐个介绍:   &…

    Linux干货 2016-11-04