8月5号 练习+作业

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

[root@localhost ~]# ifconfig |tr -cs '[:digit:].' '\n' |sort -t. -k3 |tail -5
255.0.0.0
255.255.0.0
127.0.0.1
10.10.10.10
10.10.255.255

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

[root@localhost ~]# df |tr -s ' ' % |cut -d% -f5 |sort -n |tail -1
21

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

[root@localhost ~]# cat /etc/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: -f3
1777

[root@localhost ~]# stat /tmp |head -4 |tail -1 |tr -cs '[0-9]' '\n' |head -2 |tail -1
1777

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

[root@localhost ~]# netstat -nt |tr -s ' ' ':' |cut -d: -f6 |tr -d '[:alpha:]' |sort -r |uniq -c
      2 10.10.10.1
      2

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

[root@localhost ~]# grep -i '^s' /proc/meminfo
[root@localhost ~]# grep '^[sS]' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:               240 kB
Slab:              58904 kB
SReclaimable:      32592 kB
SUnreclaim:        26312 kB

7显示/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

8显示用户rpc 默认的shell 程序

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

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

[root@localhost ~]# grep '\<[0-9]\{2,3\}' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin

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

[root@localhost ~]# grep '^[[: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"

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

[root@localhost ~]# netstat -ant |grep 'LISTEN[[:space:]]*$'
tcp        0      0 0.0.0.0:57160               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
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

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

[root@localhost ~]# grep '^\([^:]\+\>\).*/\1$' /etc/passwd
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:507:514::/home/bash:/bin/bash
nologin:x:510:517::/home/nologin:/sbin/nologin

13、显示三个用户root mage wang UID 和默认shell

[root@localhost ~]# grep -E '^(root|mage|wang)\>' /etc/passwd |cut -d: -f1,3,7
root:0:/bin/bash
mage:511:/bin/bash
wang:512:/bin/bash

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

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

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

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

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

[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o '^/.*/\b'
/etc/rc.d/init.d/
[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |egrep -o '^/.*/\<'
/etc/rc.d/init.d/

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

[root@localhost ~]# last |grep '^root\>' |tr -s ' ' |cut -d' ' -f3 |grep '^[[:digit:]]' |sort |uniq -c
     20 10.10.10.1
      2 10.1.69.10
      1 10.1.69.200
      1 10.1.69.70

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

0-9:     [0-9]
10-99:    [1-9][0-9]
100-199:  1[0-9][0-9]
200-249:  2[0-4][0-9]
250-255:  25[0-5]

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

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
127.0.0.1

20、取本机ip地址

[root@localhost ~]# ifconfig |grep '\<inet\>' |tr -s ' ' ':' |cut -d: -f4
10.10.10.10
10.1.253.35
127.0.0.1

21、取各分区利用率的数值

[root@localhost ~]# df |grep '^/dev/sd' |tr -s ' ' ':' |cut -d: -f1,5 |tr -d %
/dev/sda2:2
/dev/sda5:1
/dev/sda1:53

22、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@localhost ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' '\n' |sort |uniq -c | sort -n -r
     67 pid
     55 if
     54 file
     51 echo
     47 then
     45 return
     45 fi
     33 n

23、/etc/rc.d/init.d/functions/" 取目录名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |egrep -o '^/.*/+\b'
/etc/rc.d/init.d/

24、正则表达式表示身份证号

[1-9][0-9]{5}:前6位数字

(19[0-9][0-9]|200[0-9]|201[0-6]:4位代表年份

(0[0-9]|1[0-2]):2位代表月份

([0-2][0-9]|3[0-1]:2位代表日期

[0-9]{3}([0-9]|X):后4位随机数(包含出现尾数为X的情况)

[root@localhost ~]# echo "ID:44162219810327471X" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
44162219810327471X
[root@localhost ~]# echo "ID:441622198103274710" |egrep -o '\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>'
441622198103274710

25、正则表达式表示手机号

[root@localhost ~]# echo "phone number 18925619340" |egrep -o "\<1[3|5|7|8][0-9]{9}\>"
18925619340

26、正则表达式表示邮箱

[root@localhost ~]# cat mail |egrep '^[[:alnum:]]+@[[:alnum:]]+.com\>'
wangwu@163.com
123456@qq.com
li23si@sohu.com

27、正则表达式表示QQ号

  QQ号5-10位:

[1-9]:第一个数字不能为0
[0-9]{4,9}:后面跟4-9位数字
[root@localhost ~]# echo "My QQ is 9087654321" |egrep -o "\<[1-9][0-9]{4,9}\>"
9087654321
[root@localhost ~]# echo "My QQ is 54321" |egrep -o "\<[1-9][0-9]{4,9}\>"
54321

 


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

(0)
pingskypingsky
上一篇 2016-08-07 22:05
下一篇 2016-08-07 22:05

相关推荐

  • 2016/10/19作业:软链接与硬链接的区别

    软链接和硬链接的区别 linux中链接分为两种,一种是硬链接(Hard Link),一种是符号链接(Symbolic Link),其中,符号链接又称为软链接。 硬链接: 在Linux文件系统中 ,磁盘中的所有文件都有一个编号,这个编号称为索引节点(Inode)。在Linux中,多个同一索引节点可以有多个文件名,这就是硬链接。硬链…

    Linux干货 2016-10-20
  • 让自定义脚本成为服务脚本

    1.脚本注释格式:此格式能让chkconfig命令识别 #!/bin/bash#chkconfig:runlevel [S]##  [K]##    定义默认runlevel) (S开头,定义启动优先级) (K开头,定义关闭优先级) #description:脚本说明:太长的话需要\换行 2.case语句实现start,stop…

    Linux干货 2017-05-15
  • Linux Services and Security–part1

    一、详细描述一次加密通讯的过程,结合图示最佳 以Bob和Alice安全通讯为例: Bob<———>Alice 1. Bob要和Alice安全通信首先要取得对方的公钥,即对方的证书,并验证证书的合法性。验证过程和内容: 1)、用CA的公钥(双方已知)解密对方证书中CA的签名;能解密说明证书来原可靠; 2)、用证书中标记的“签名算法”来计算证书的相关…

    2017-10-17
  • 推荐-内核虚拟化技术——LXC初体验

    一、Cgroups 1.1 介绍 Cgroup是Control group的简称。最初由由Google的工程师(主要是Paul Menage和Rohit Seth)在2006年以“process containers(进程容器)”的名字开始的, 在2007年的晚些时候被重命名为“控制组”并被合并到了2.6.24版的内核中,现已成为Linux内核中的一个功能,…

    Linux干货 2016-03-31
  • History的作用及调用历史命令的快捷方式

    History的作用及调用历史命令的快捷方式 M21-陆东贵 CentOS 7.2 命令历史:shell进程会在其回话中保存此前用户提交执行过的命令; history命令:命令历史; 命令语法:history [-c] [-d 偏移量] [n] 或           …

    Linux干货 2016-10-19
  • 第九周博客作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现 #!/bin/bash declare -i sum=0 declare -i sum_nologin=0 for i in $(cut -d: -f7 /etc/passwd);do if…

    Linux干货 2017-06-06