本周作业内容:

1、显示当前系统上root、fedora或user1用户的默认shell;

[root@captain ~]# grep -wE '^(root|fedora|user1)' /etc/passwd |\
> awk -F'[:]' 'BEGIN{printf("username\tshell\n")}\
> {printf("%-15s\t%s\n",$1,$NF)}'
username        shell
root            /bin/bash


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

[root@captain ~]# egrep -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()


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

[root@captain ~]# echo "/ab/12/cd/45ef" | grep -Eo '[^/.]*$'
45ef

    扩展:取出其路径名

[root@captain ~]# echo "/ab/12/cd/45ef" | grep -Eo '^/.*/' | grep -o '^/.*[^/]'
/ab/12/cd

使用命令

[root@captain ~]# basename "/ab/12/cd/45ef"
45ef
[root@captain ~]# dirname "/ab/12/cd/45ef"
/ab/12/cd

另外可以使用bash的字符串截取功能

[root@captain ~]# s="/ab/12/cd/45ef"
[root@captain ~]# echo ${s##*/}
45ef
[root@captain ~]# echo ${s%/*}
/ab/12/cd

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

[root@captain ~]# ifconfig | egrep -o '\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\b'


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

[root@captain ~]# cat /home/shell/iplist
1       192.168.1.100
2       192.168.100.20
3       11117.23.34.45
4       2559.23.34.123
5       255.255.255.0
6       255.255.248.0.12
7       255.255.240.0
8       1234.234.123.324
9       10.10.10.123.
10      224.234.23.123.34
11      0.0.0.0
12      10.10.10.10
13      12312.342432.12312.23

[root@captain ~]# egrep -o '\<(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\>' /home/shell/iplist
192.168.1.100
192.168.100.20
255.255.255.0
255.255.248.0
255.255.240.0
10.10.10.123
224.234.23.123
0.0.0.0
10.10.10.10


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

[root@captain ~]# cat /home/shell/malist 
abc@afd.com
fdasfa@gmail.com
abc@afd@com
abc.@gmail.com
abc.df@gmail.com
abcr_.df@gmail.com
abcr_.df@gmail.com.123
fas@.asdf@
fas.@asdf.df

[root@captain ~]# egrep -o '[[:alnum:]_\.]+@[[:alnum:]]+\.[[:alnum:]_\.]*[a-z]+' /home/shell/malist 
abc@afd.com
fdasfa@gmail.com
abc.@gmail.com
abc.df@gmail.com
abcr_.df@gmail.com
abcr_.df@gmail.com
fas.@asdf.df


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

[root@captain ~]# find /var/ \( -user root -a -group mail \) -ls
782573    4 drwxrwxr-x   2 root     mail         4096 Aug 29 16:08 /var/spool/mail
792338   12 -rw-------   1 root     mail         8666 Aug 23 17:04 /var/spool/mail/root


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

[root@captain ~]# find / -type f \( -nouser -o -nogroup \) -ls

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

[root@captain ~]# find / -type f -atime +3 -a \( -nouser -o -nogroup \) -ls

 

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

[root@captain ~]# find /etc/ -perm -222 -ls


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

[root@captain ~]# find /etc/ -type f -size +1M -ls


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

[root@captain ~]# find /etc/init.d/ \( -perm -111 -a -perm -002 \) -ls


12、查找/usr目录下不属于root、bin或hadoop的文件;

[root@captain ~]# find /usr/ -type f -not  \( -user root -o -user bin -o -user hadoop \) -ls


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

[root@captain ~]# find /etc -type f -not -perm -222 -ls


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

[root@captain ~]# find /etc/ -type f -mtime -7 -not \( -user root -o -user hadoop \) -ls