Linux Basic 5: 正規表達式及find命令

第五週作業內容:

1、顯示當前系統上root、fedora或user1用戶的默認shell;

# nano ~/usershell.sh

#!/bin/bash
#

for userid in {root,fedora,user1};do
    id $userid &> /dev/null || useradd $userid && grep "^$userid" /etc/passwd | cut -d: -f1,7
done

# bash -n ~/usershell.sh
# bash -x ~/usershell.sh
# chmod +x !$
# bash !$

root:/bin/bash

fedora:/bin/bash

user1:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中某單詞後面跟一組小括號的行,形如:hello();

# grep "\<[[:alpha:]]\+\>()" /etc/rc.d/init.d/functions

checkpid() {

daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

3、使用echo命令輸出一個絕對路徑,使用grep取出其基名; 擴展:取出其路徑名

# echo "/etc/rc.d/init.d/functions" | grep -o "[^/]\+$"
# echo "/etc/rc.d/init.d/functions/" | grep -o "[^/]\+/\?$" | tr -d '/'

functions

# echo "/etc/rc.d/init.d/functions" | grep  -o "/.*/"
# echo "/etc/rc.d/init.d/functions/" | cut -d'/' -f1,2,3,4

/etc/rc.d/init.d

4、找出ifconfig命令結果中的1-255之間數字;

# ifconfig | grep -E -o --color '\b(2[0-4][0-9])\b|\b(25[0-5])\b|\b(1[0-9][0-9])\b|\b([1-9][0-9])\b|\b([1-9])\b'

29
83
192
168
1
102
192
168
1
255
255
255
255
64
1
1
127
1
255
1
128
1
4
4
240
240
240
240

5、挑戰題:寫一個模式,能匹配合理的IP地址;

擴展正規表達式的模式:

"\b(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-3][0-9]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-4]))\b"

示例:

# ifconfig | grep -E  --color "\b(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-3][0-9]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-5]))\.(([1-9])|([1-9][0-9])|(1[0-9]+)|(2[0-4][0-9])|(25[0-4]))\b"

          inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0

          inet addr:127.0.0.1  Mask:255.0.0.0

說明:排除了主機位全1(廣播地址)和主機全0(網絡位)

6、挑戰題:寫一個模式,能匹配出所有的郵件地址;

擴展正規表達式的模式:

'^([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})@(([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})\.){1,5}([[:alnum:]]{0,}[-_]?[[:alnum:]]{0,})$'

7、查找/var目錄下屬主爲root,且屬組爲mail的所有文件或目錄;

# find /var -user root -group mail

/var/spool/mail

8、查找當前系統上沒有屬主或屬組的文件;

    進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;

# find / -nouser -o -nogroup -atime -3 | ls -l

-rw-rw----. 1 admin     mail 0 7月  31 23:39 admin

-rw-rw----. 1 asd       mail 0 8月  24 21:30 asd
-rw-rw----. 1 bash      mail 0 8月  25 12:07 bash
-rw-rw----. 1 basher    mail 0 8月  25 12:07 basher
-rw-rw----. 1 fedora    mail 0 9月   1 07:57 fedora
-rw-rw----. 1 gdm       mail 0 8月  24 20:37 gdm
-rw-rw----. 1 gentoo    mail 0 8月  24 21:36 gentoo
-rw-rw----. 1 hadoop    mail 0 8月  21 21:37 hadoop
-rw-rw----. 1 lll       mail 0 8月  24 21:31 lll
-rw-rw----. 1 mageia    mail 0 8月  21 18:40 mageia
-rw-rw----. 1 nologin   mail 0 8月  25 12:07 nologin
-rw-rw----. 1 openstack mail 0 8月  21 18:53 openstack
-rw-rw----. 1 rpc       mail 0 7月  31 22:22 rpc
-rw-rw----. 1 slackware mail 0 8月  21 18:48 slackware
-rw-rw----. 1 testbash  mail 0 8月  25 12:07 testbash
-rw-rw----. 1 user1     mail 0 9月   1 07:57 user1

9、查找/etc目錄下所有用戶都有寫權限的文件;

# find /etc -perm /222

10、查找/etc目錄下大於1M,且類型爲普通文件的所有文件;

# find /etc -type f -size +1M

/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

/etc/selinux/targeted/modules/active/policy.kern
/etc/selinux/targeted/policy/policy.24

11、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件;

# find /etc/init.d -perm /113

/etc/init.d

12、查找/usr目錄下不屬於root、bin或hadoop的文件;

# find /usr -not \( -user root -o -user bin -o -user hadoop \)
# find /usr -not -user root -a -not -user bin -a -not -user hadoop

/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

13、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;

# find /etc -not -perm /222

/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem

/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/java/cacerts
/etc/shadow-
/etc/shadow
/etc/pam.d/cups
/etc/openldap/certs/password
/etc/gshadow
/etc/gshadow-
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/thin-performance.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/ld.so.conf.d/kernel-2.6.32-642.el6.x86_64.conf
/etc/dbus-1/system.d/cups.conf
/etc/rc.d/init.d/blk-availability
/etc/rc.d/init.d/lvm2-lvmetad
/etc/rc.d/init.d/lvm2-monitor
/etc/sudoers

14、查找/etc目錄下最近一週內其內容被修改過,且不屬於root或hadoop的文件;

# find /etc -mtime -7 -not -user root -a -not -user hadoop
# find /etc -mtime -7 -not \( -user root -user hadoop \)

/etc/aa.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章