码迷,mamicode.com
首页 > 其他好文 > 详细

23 第五周作业

时间:2016-09-05 17:36:58      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:linux   作业   

1、显示当前系统上rootfedorauser1用户的默认shell

答:Linux的哲学思想之一:组合单一目的的小程序,完成复杂任务;
第一步:使用扩展正则表达式egrep或grep –E显示出匹配到的用户信息;
^ :行首锚定,用于模式的最左侧;\>或\b:词尾锚定,用于单词模式的右侧;
[root@ilinux~]# grep -E"^root\>|^fedora\>|^user1\>" /etc/passwd   
root:x:0:0:root:/root:/bin/bash
user1:x:508:508::/home/user1:/bin/bash
第二步:使用cut –d: 以冒号为分隔符,显示出匹配到的用户名及默认shell;
[root@ilinux~]# grep -E"^root\>|^fedora\>|^user1\>" /etc/passwd | cut -d: -f1,7
root:/bin/bash
user1:/bin/bash


2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello()

答:
方法一:使用正则表达式grep过滤,-o 只显示匹配到的行,()不需要转义;
[[:alpha:]] 表示任意大小写字母;\+:匹配其前面的字符至少1次;
[root@ilinux~]# grep -o "[[:alpha:]]\+()"/etc/rc.d/init.d/functions 
str()
checkpid()
readlink()
fgrep()
loop()
loop()
run()
pidof()
daemon()
killproc()
pidfileofproc()
pidofproc()
status()
success()
failure()
passed()
warning()
stage()
success()
failure()
passed()
warning()
action()
strstr()
confirm()
dev()
file()
true()
false()
sysctl()
random()
point()
crypto()
方法二:使用扩展正则表达式egrep或grep –E;()需要转义,写成\(\);
+:匹配其前面的字符至少1次;
[root@ilinux~]# egrep -o"[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions


3、使用echo命令输出一个绝对路径,使用grep取出其基名;扩展:取出其路径名;

答:
1)取出其基名
 [root@ilinux ~]# echo "/tmp/mydir/text.txt" | grep -Eo "[^/]+/?$"
text.txt
[root@ilinux~]# echo "/tmp/mydir/text.txt"| grep -Eo "[^/]+/?$" | cut -d"/" -f1
text.txt
说明:
[^]:指定范围外的任意单个字符;+:匹配其前面字符至少1次;
? :匹配其前面字符0次或1次;$ :行尾锚定,用于模式的最右侧;
/?是考虑到路径可能为目录;

2)取出其路径名
法一:
[root@c66-moban~]# echo "/tmp/mydir/text.txt"| grep -o "/[A-Za-z0-9]\+.*/"
/tmp/mydir/
法二:
[root@c66-moban~]# echo "/tmp/mydir/text.txt"| grep -o "/[[:alnum:]]\+.*/"
/tmp/mydir/
法三:使用egrep或grep –E
[root@c66-moban~]# echo "/tmp/mydir/text.txt"| egrep -o "/[[:alnum:]]+.*/"
/tmp/mydir/
说明:
[A-Za-z0-9]或[[:alnum:]] :表示任意单个字母或者数字;.*:任意长度的任意字符;
在基础正则表达式中,\+:匹配其前面的字符至少1次;
在扩展正则表达式中,+:匹配其前面的字符至少1次;
 
当然,还有一种获取基名和路径名的简单方式:
[root@c66-moban~]# basename /tmp/mydir/text.txt    #获取基名;
text.txt
[root@c66-moban~]# dirname /tmp/mydir/text.txt     #获取路径名;
/tmp/mydir


4、找出ifconfig命令结果中的1-255之间数字;

答:第一步,先用ifconfig命令查看下内容;
[root@c66-moban~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:02:12:34 
          inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr:fe80::20c:29ff:fe02:1234/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500 Metric:1
          RX packets:5477 errors:0 dropped:0overruns:0 frame:0
          TX packets:4759 errors:0 dropped:0overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:509600 (497.6 KiB)  TX bytes:2006936 (1.9 MiB)
 
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536 Metric:1
          RX packets:0 errors:0 dropped:0overruns:0 frame:0
          TX packets:0 errors:0 dropped:0overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
第二步:需要把1-255分段处理,故分为:1-9,10-99,100-199,200-249,250-255;
[root@c66-moban ~]# ifconfig | egrep -o "\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]"       
29
12
34
192
168
1
110
192
168
1
255
255
255
255
64
1
9
1
9
127
1
255
1
128
1


5、挑战题:写一个模式,能匹配合理的IP地址;

答:
[root@c66-moban ~]# ifconfig | egrep -o"(\<[1-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)\.(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>).(\<[0-9]\>|\<[1-9][0-9]\>|\<[1][0-9][0-9]\>|\<[2][0-4][0-9]\>|\<[2][5][0-5]\>)"
192.168.1.110
192.168.1.255
255.255.255.0
127.0.0.1
255.0.0.0


6、挑战题:写一个模式,能匹配出所有的邮件地址;

答:
法一:
[root@c66-moban ~]# echo "email: test2016@110.com" | grep -o"\<[[:alnum:]]\+.*@[0-9a-z]\+\.[[:alpha:]]\+\>"
email: test2016@110.com
法二:
[root@c66-moban ~]# echo "email: 110test2016@110.com" | egrep -o"\<[[:alnum:]]+.*@[0-9a-z]+\.[[:alpha:]]+\>"
email: 110test2016@110.com
说明:
\<或\b:词首锚定,用于单词模式的左侧;\>或\b:词尾锚定,用于单词模式的右侧;
.*:任意长度的任意字符;\ . 是表示“.”本身;


7、查找/var目录下属主为root,且属组为mail的所有文件或目录;

答:
[root@c66-moban~]# find /var -user root -group mail -ls
261213    4 drwxrwxr-x   2 root    mail         4096 Aug 11 21:42/var/spool/mail


8、查找当前系统上没有属主或属组的文件;

答:
[root@c66-moban~]# find / -nouser -a -nogroup 
find:`/proc/4071/task/4071/fd/5‘: No such file or directory
find:`/proc/4071/task/4071/fdinfo/5‘: No such file or directory
find:`/proc/4071/fd/5‘: No such file or directory
find:`/proc/4071/fdinfo/5‘: No such file or directory

进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

答:
[root@c66-moban~]# find / -nouser -a -nogroup -a -atime-3
find:`/proc/4072/task/4072/fd/5‘: No such file or directory
find:`/proc/4072/task/4072/fdinfo/5‘: No such file or directory
find:`/proc/4072/fd/5‘: No such file or directory
find:`/proc/4072/fdinfo/5‘: No such file or directory
说明:
-atime[+|-]# :按访问时间查找;以“天”为单位;#表示数字,天数;
         # :[#,#+1)
         +# :[#+1,oo)
         -# :[0,#)


9、查找/etc目录下所有用户都有写权限的文件;

答:
[root@ilinux ~]# find /etc/-perm -222 -ls
说明:
-perm-MODE:每一类对象都必须同时拥有为其指定的权限标准;与关系;
      例如:-perm -666,那么766也符合标准;
-perm/MODE :任何一类(u,g,o)对象的权限中只要能有一位匹配即可;或关系;
    注意:在centos7上要用“/MODE”,+MODE不能使用;


10、查找/etc目录下大于1M,且类型为普通文件的所有文件;

答:
[root@ilinux~]# find /etc/ -size +1M -a -type f -ls
14494697892 -rw-r--r-- 1 roo root 8080641 Aug 3 2015 /etc/selinux/targeted/policy/policy.24
1449466 7892 -rw-r--r-- 1 root root  8080641  Aug  3  2015/etc/selinux/targeted/modules/active/policy.kern
说明:
-size [+|-]#UNIT  :根据文件大小查找;#表示数字大小,UNIT表示单位;
    常用单位:k,M,G
    #UNIT :(#-1,#]          
    -#UNIT :[0,#-1]
    +#UNIT :(#,oo)
-typeTYPE:根据文件类型查找;
  f :普通文件;
  d :目录文件;
  l :符号链接文件;
  s :套接字文件;
  b :块设备文件;
  c :字符设备文件;
  p :管道文件;


11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;

答:
[root@c66-moban~]# find /etc/init.d  -perm -113 -ls
131027  0 lrwxrwxrwx   1 root  root   11 Jun 27 2015 /etc/init.d -> rc.d/init.d
说明:
-perm-MODE:每一类对象都必须同时拥有为其指定的权限标准;与关系;


12、查找/usr目录下不属于rootbinhadoop的文件;

答:
[root@ilinux~]# find /usr/ -not \( -user root -o-user bin -o -user hadoop \) -ls 
267080   12 -rwsr-xr-x   1 abrt    abrt     10296 Oct 16  2014/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
263399    0 -rw-r--r--   1 user10  user10      0 Aug 28 01:06/usr/user10.txt


13、查找/etc/目录下至少有一类用户没有写权限的文件;

答:
[root@c66-moban~]# find /etc -not -perm -222


14、查找/etc目录下最近一周内其内容被修改过,且不属于roothadoop的文件;

答:
方法一:
[root@ilinux~]# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) 
方法二:
[root@ilinux~]# find /etc -mtime -7 -a -not -user root -a -not -user hadoop

本文出自 “ilinux” 博客,请务必保留此出处http://shenzhijin.blog.51cto.com/1741240/1846333

23 第五周作业

标签:linux   作业   

原文地址:http://shenzhijin.blog.51cto.com/1741240/1846333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!