标签:第五周作业
1、显示当前系统上root、fedora或user1用户的默认shell;
# /etc/passwd文件中每行第一字段为用户名,第七字段为默认bash,使用^进行行首锚定,然后使用egrep中的正则表达式元字符“|”来匹配,使用cut命令切割出第一和第七字段得到结果。 [root@localhost ~]# egrep "^root|fedora|user1" /etc/passwd | cut -d":" -f 1,7 root:/bin/bash user1:/bin/bash fedora:/bin/bash [root@localhost ~]# cat /etc/passwd #通过cat命令查看文档进行确认 root:x:0:0:root:/root:/bin/bash #root用户 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 halt:x:7:0:halt:/sbin:/sbin/halt 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 avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:997:995:User for polkitd:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin user1:x:1000:1000::/home/user1:/bin/bash #user1用户 hadoop:x:2000:2000::/home/hadoop:/bin/bash fedora:x:2001:2001::/home/fedora:/bin/bash #fedora用户 #经检查结果符合要求;
2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
#此处可以使用grep或者egrep命令,单词,可以使用 -i "\<[a-z]\>\+"或者"[[:alpha:]]\+"实现
[root@localhost ~]# grep "\<[[:alpha:]]\+()" /etc/rc.d/init.d/functions
#单词需词首词尾锚定用"\<","\>",至少需要一个大写或小写字母用"\+"
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
#使用egrep命令有一点要注意()本身代表分组的意思因此需要使用转义符
[root@localhost ~]# egrep -i "\<[a-z]+\>\(\)" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {3、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-eno16777736/ | grep -Eo "\<[^/]+/?$" | grep -Eo ".*[^/]" ifcfg-eno16777736
扩展:取出其路径名
[root@localhost ~]# echo /etc/sysconfig/network-scripts/ifcfg-eno16777736/ | grep -Eo "^/.*/[^/]+" | grep -Eo "/.*/" | grep -Eo "^/.*[^/]+" /etc/sysconfig/network-scripts
4、找出ifconfig命令结果中的1-255之间数字;
[root@yang ~]# ifconfig | grep -Eo "\<[1-9]\>|\<[1-9][0-9]\>|\<1[0-9]{2}\>|\<2[0-4][0-9]\>|\<25[0-5]\>"
#\<[1-9]\>匹配1-9
#\<[1-9][0-9]\>匹配10-99
#\<1[0-9]{2}\>匹配100-199
#<2[0-4][0-9]\>匹配200-249
#\<25[0-5]\>匹配250-255
29
50
192
168
188
192
168
255
255
255
255
64
1
205
1
231
5
127
1
255
1
128
15、挑战题:写一个模式,能匹配合理的IP地址;
[root@yang ~]# ifconfig | egrep -o "(\<([0,1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0,1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\>"
192.168.0.188
192.168.0.255
255.255.255.0
127.0.0.1
255.0.0.06、挑战题:写一个模式,能匹配出所有的邮件地址;
[root@yang ~]# grep ".*@.*\.[[:alpha:]]\+$" /test/mail.txt py0426@qq.com py0426@163.com nihao@126.cn
7、查找/var目录下属主为root,且属组为mail的所有文件或目录;
[root@yang ~]# find /var -user root -a -group mail -ls 393927 4 drwxrwxr-x 2 root mail 4096 Aug 2 02:45 /var/spool/mail
8、查找当前系统上没有属主或属组的文件;
[root@yang ~]# find / -type f -nouser -a -nogroup -ls find: `/proc/4812/task/4812/fd/5‘: No such file or directory find: `/proc/4812/task/4812/fdinfo/5‘: No such file or directory find: `/proc/4812/fd/5‘: No such file or directory find: `/proc/4812/fdinfo/5‘: No such file or directory
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
find / -nouser -a -nogroup -a -atime -3
9、查找/etc目录下所有用户都有写权限的文件;
[root@yang ~]# find /etc -type f -perm -222
10、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[root@yang ~]# find /etc -size +1M -type f -exec ls -lh {} \;
-rw-r--r--. 1 root root 1.3M Mar 31 07:53 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
-rw-r--r--. 1 root root 8.0M Mar 30 18:46 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 8.0M Mar 30 18:46 /etc/selinux/targeted/policy/policy.24
-rw-r--r--. 1 root root 1.1M Apr 24 2015 /etc/pki/tls/certs/ca-bundle.trust.crt11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@yang ~]# find /etc/init.d/ -perm 113
12、查找/usr目录下不属于root、bin或hadoop的文件;
[root@yang ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop \) -ls | tail -5 664441 12 -rwsr-xr-x 1 abrt abrt 10296 Jul 25 2015 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache [root@yang ~]#
13、查找/etc/目录下至少有一类用户没有写权限的文件;
[root@yang ~]# find /etc/ -not -perm -222 -ls | tail -5 791666 4 -rwxr-xr-x 1 root root 1687 Apr 10 2015 /etc/ppp/ipv6-down 791662 4 -rwxr-xr-x 1 root root 386 Apr 10 2015 /etc/ppp/ip-down 791665 8 -rwxr-xr-x 1 root root 6517 Apr 10 2015 /etc/ppp/ip-up.ipv6to4 796940 4 -rw-r--r-- 1 root root 5 Mar 16 2015 /etc/ppp/options 787717 4 -rw-r--r-- 1 root root 801 Jul 19 2011 /etc/gssapi_mech.conf
14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[root@yang ~]# find -mtime -7 -not \( -user root -o -user hadoop \)
标签:第五周作业
原文地址:http://xiaoqiqingfeng.blog.51cto.com/3720264/1846530