本周作业内容:
1、显示当前系统上root、fedora或user1用户的默认shell;
[[email protected] ~]# 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();
[[email protected] ~]# 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取出其基名;
[[email protected] ~]# echo "/ab/12/cd/45ef" | grep -Eo ‘[^/.]*$‘ 45ef
扩展:取出其路径名
[[email protected] ~]# echo "/ab/12/cd/45ef" | grep -Eo ‘^/.*/‘ | grep -o ‘^/.*[^/]‘ /ab/12/cd
使用命令
[[email protected] ~]# basename "/ab/12/cd/45ef" 45ef [[email protected] ~]# dirname "/ab/12/cd/45ef" /ab/12/cd
另外可以使用bash的字符串截取功能
[[email protected] ~]# s="/ab/12/cd/45ef" [[email protected] ~]# echo ${s##*/} 45ef [[email protected] ~]# echo ${s%/*} /ab/12/cd
4、找出ifconfig命令结果中的1-255之间数字;
[[email protected] ~]# ifconfig | egrep -o ‘\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\b‘
5、挑战题:写一个模式,能匹配合理的IP地址;
[[email protected] ~]# 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 [[email protected] ~]# 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、挑战题:写一个模式,能匹配出所有的邮件地址;
[[email protected] ~]# cat /home/shell/malist [email protected] [email protected] [email protected]@com [email protected] [email protected] [email protected] [email protected] [email protected]@ [email protected] [[email protected] ~]# egrep -o ‘[[:alnum:]_\.][email protected][[:alnum:]]+\.[[:alnum:]_\.]*[a-z]+‘ /home/shell/malist [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
7、查找/var目录下属主为root,且属组为mail的所有文件或目录;
[[email protected] ~]# 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、查找当前系统上没有属主或属组的文件;
[[email protected] ~]# find / -type f \( -nouser -o -nogroup \) -ls
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
[[email protected] ~]# find / -type f -atime +3 -a \( -nouser -o -nogroup \) -ls
9、查找/etc目录下所有用户都有写权限的文件;
[[email protected] ~]# find /etc/ -perm -222 -ls
10、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[[email protected] ~]# find /etc/ -type f -size +1M -ls
11、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[[email protected] ~]# find /etc/init.d/ \( -perm -111 -a -perm -002 \) -ls
12、查找/usr目录下不属于root、bin或hadoop的文件;
[[email protected] ~]# find /usr/ -type f -not \( -user root -o -user bin -o -user hadoop \) -ls
13、查找/etc/目录下至少有一类用户没有写权限的文件;
[[email protected] ~]# find /etc -type f -not -perm -222 -ls
14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[[email protected] ~]# find /etc/ -type f -mtime -7 -not \( -user root -o -user hadoop \) -ls
时间: 2024-10-13 18:42:15