linux-practice(23-24)
23、grep/egrep正則表達式練習:
1) 顯示/etc/passwd文件中不以bash結尾的行;
答:
# grep -v "bash$" /etc/passwd
2) 找出ifconfig命令執行結果中的兩位或三位整數;
答:
# ifconfig | grep --color=auto "\<\([[:digit:]]\{2,3\}\)\>" //還有00
# ifconfig | grep --color=auto "\<\([1-9][0-9]\|[1-9][0-9][0-9]\)\>" //完美
3) 找出/etc/grub2.cfg(在CentOS6中是/etc/grub.conf)文件中,以空白字符開頭的非空白行;
答:
# cat /etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$" //centos6
# grep "^[[:space]]\+[^[:space:]]\+" /etc/grub2.cfg //centos7
default=0
設定默認啟動菜單項;整個菜單中的所有的title是從0開始編號的;
timeout=5
用戶未作出選擇時,grub會自動引導默認菜單對應的內核的超時時間;
splashimage=(hd0,0)/grub/splash.xpm.gz
grub的背景圖片對應的路徑;
hiddenmenu
隱藏grub的啟動菜單
password --md5 CRYPTED_PASSWORD
為了保護菜單設置的密碼,通常用於防止隨意進入單用戶模式;
title CentOS 6 (2.6.32-573.el6.x86_64)
定義菜單項中的各個"標題",可以定義多個title項;至少有一項;
root (hd0,0)
指定grub的根設備,通常是用來安裝grub的那個分區;
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=5ebb0e76-bb19-4a80-9c70-0d101c0778e1 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
該菜單對應的要啟動的內核及傳遞給內核的各個參數;
initrd /initramfs-2.6.32-573.el6.x86_64.img
內核所對應的ramdisk(ramfs)文件
password --md5 CRYPTED_PASSWORD
保護操作系統內核的啟動;
4) 找出"netstat -tan”命令的結果中,以"LISTEN"後跟任意個空白字符結尾的行;
答:
# netstat -tan | grep ".*LISTEN[[:space:]]*$"
# netstat -tan | grep "LISTEN[[:space:]]*$"
5) 找出"fdisk -l"命令的結果中,包含以/dev/後跟sd或hd及一個小寫字母的行;
答:
# fdisk -l | grep "^/dev/[s|h]d[a-z]"
6) 找出"ldd /usr/bin/vim"命令的結果中所有的文件路徑;
答:
# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+"
# ldd /usr/bin/vim | grep -o "/.*/.*"
7) 找出/proc/meminfo文件中,所有以大寫S或小寫s開頭的行,你有多少種方法實現該任務?
答:
# cat /proc//meminfo | grep "^[Ss].*" //內存使用情況
# cat /proc//meminfo | grep -E "^(S|s).*"
# cat /proc//meminfo | grep -i "^s" //忽略大小寫
# cat /proc/meminfo | grep "^s\|^S" //註意解釋輸出“或”
# cat /proc/meminfo | grep -e "^s" -e "^S" //多項匹配
8) 使用egrep取出某個路徑的基名;
答:
# ls /usr/bin/ldd | grep -o "/[a-z]\+$"
9) 顯示出ifconfig命令結果中的0-255之間的整數;
答:
# ifconfig | grep --color=auto "\<\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\)\>"
10) 添加用戶bash, testbash, basher, nologin, unlogin, logining,要求前三個用戶的默認shell為/bin/bash,後三個用戶的默認shell為/sbin/nologin,而後找出其用戶名與shell名相同的用戶信息;
答:
# useradd -s /bin/bash bash
# useradd -s /bin/bash basher
# useradd -s /bin/bash testbash
# useradd -s /sbin/nologin nologin
# useradd -s /sbin/nologin unlogin
# useradd -s /sbin/nologin logining //創建多用戶可用腳本來完成
# cat /etc/passwd | grep "^\<\([[:alnum:]]\+\)\>.*\1$"
例:
[root@little ~]# grep -v "bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
……
[root@little ~]# ifconfig | grep --color=auto "\<\([[:digit:]]\{2,3\}\)\>"
eth0 Link encap:Ethernet HWaddr 00:0C:29:EF:91:5E
inet addr:172.16.67.2 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:feef:915e/64 Scope:Link
RX bytes:638912 (623.9 KiB) TX bytes:587812 (574.0 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:EF:91:68
inet addr:192.168.222.136 Bcast:192.168.222.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:9168/64 Scope:Link
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:112446 (109.8 KiB) TX bytes:1334 (1.3 KiB)
eth2 Link encap:Ethernet HWaddr 00:0C:29:EF:91:72
inet addr:192.168.222.137 Bcast:192.168.222.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feef:9172/64 Scope:Link
TX packets:175 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:120635 (117.8 KiB) TX bytes:17648 (17.2 KiB)
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
RX packets:46 errors:0 dropped:0 overruns:0 frame:0
TX packets:46 errors:0 dropped:0 overruns:0 carrier:0
[root@little ~]#
[root@little ~]# cat /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda1
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-573.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-573.el6.x86_64.img
[root@little ~]#
[root@little ~]# cat /etc/grub.conf | grep "^[[:space:]]\+.*[^[:space:]]$"
root (hd0,0)
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=1e8379ed-1b71-4c70-b97a-4d2121654719 nomodeset rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-573.el6.x86_64.img
[root@little ~]#
[root@little ~]# netstat -tan | grep ".*LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:36239 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp 0 0 :::111 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::33813 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 ::1:6010 :::* LISTEN
[root@little ~]#
[root@little ~]# fdisk -l | grep "^/dev/[s|h]d[a-z]"
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 6553 52428800 83 Linux
/dev/sda3 6553 6814 2097152 82 Linux swap / Solaris
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/.*/.*"
/lib64/libm.so.6 (0x0000003634800000)
/lib64/libselinux.so.1 (0x0000003635400000)
/lib64/libtinfo.so.5 (0x000000363d000000)
/lib64/libacl.so.1 (0x0000003c76800000)
/usr/lib64/libgpm.so.2 (0x0000003634c00000)
/usr/lib64/perl5/CORE/libperl.so (0x0000003634400000)
/lib64/libdl.so.2 (0x0000003633800000)
/lib64/libpthread.so.0 (0x0000003634000000)
/lib64/libc.so.6 (0x0000003633c00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003633400000)
/lib64/libattr.so.1 (0x0000003643800000)
/lib64/libresolv.so.2 (0x0000003635800000)
/lib64/libnsl.so.1 (0x0000003644c00000)
/lib64/libcrypt.so.1 (0x000000363f800000)
/lib64/libutil.so.1 (0x0000003643c00000)
/lib64/libfreebl3.so (0x000000363f400000)
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+"
/lib64/libm.so.6
/lib64/libselinux.so.1
/lib64/libtinfo.so.5
/lib64/libacl.so.1
/usr/lib64/libgpm.so.2
/usr/lib64/perl5/CORE/libperl.so
/lib64/libdl.so.2
/lib64/libpthread.so.0
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
/lib64/libattr.so.1
/lib64/libresolv.so.2
/lib64/libnsl.so.1
/lib64/libcrypt.so.1
/lib64/libutil.so.1
/lib64/libfreebl3.so
[root@little ~]#
[root@little ~]# ldd /usr/bin/vim | grep -o "/[^[:space:]]\+" | wc -l
16
[root@little ~]# ldd /usr/bin/vim | grep -o "/.*/.*" | wc -l
16
[root@little ~]# cat /proc//meminfo | grep "^[S|s].*"
SwapCached: 48 kB
SwapTotal: 2097148 kB
SwapFree: 2094960 kB
Shmem: 3136 kB
Slab: 85056 kB
SReclaimable: 15576 kB
SUnreclaim: 69480 kB
[root@little ~]# ls /usr/bin/ldd | grep -o "/[a-z]\+$"
/ldd
[root@little ~]#
[root@little ~]# ifconfig | grep --color=auto "\<\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|25[0-5]\)\>"
[root@little ~]#
[root@little ~]# cat /etc/passwd | grep "^\<\([[:alnum:]]\+\)\>.*\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@little ~]#
24.腳本練習:
1) 編寫一個腳本,要求:
統計/etc, /var, /usr目錄中所有非隱藏的一級子目錄和文件的數量總和;
答:
#!/bin/bash
#
a=$(ls /etc | wc -l)
b=$(ls /var | wc -l)
c=$(ls /usr | wc -l)
count=$[$a+$b+$c]
echo "$count"
[root@little ~]# vi 24p1.sh
[root@little ~]#
[root@little ~]# chmod +x 24p1.sh
[root@little ~]#
[root@little ~]# ./24p1.sh
303
2) 編寫一個腳本,要求:
對於目錄/tmp/testdir1,如果當前有效用戶有讀、寫和執行的權限,那麽就在該目錄中創建一個文件;否則,顯示“沒有權限不能創建文件”的信息。
答:
#!/bin/bash
#
FPATH=/tmp/testdir1
[ -r $FPATH -a -w $FPATH -a -x $FPATH ] && touch $FPATH/newdir || echo "Don‘t right to create useradd"
[root@little ~]# vi 24p2.sh
[root@little ~]#
[root@little ~]# ll /tmp/testdir1
total 0
[root@little ~]# chmod +x 24p
24p1.sh 24p2.sh
[root@little ~]# chmod +x 24p2.sh
[root@little ~]#
[root@little ~]# ./24p2.sh
[root@little ~]#
[root@little ~]# ll /tmp/testdir1
total 0
-rw-r--r--. 1 root root 0 Nov 21 11:29 newdir
[root@little ~]#
3) 編寫一個腳本,要求:
從/etc/passwd中UID和GID相同的用戶中隨機選擇一個用戶,判斷其用戶名和基本組名是否相同;
答:
#!/bin/bash
#
username=$(grep "\<\([[:digit:]]\+\).*\1\>" /etc/passwd | sort -R | head -1 | cut -d":" -f1)
groupname=$(id -gn $username)
[ $username == $groupname ] && echo "username and group is same." || "username and groupname is different."
[root@little ~]# vi 24p3.sh
[root@little ~]#
[root@little ~]# chmod +x 24p3.sh
[root@little ~]#
[root@little ~]# ./24p3.sh
cut: invalid option -- ‘1‘
Try `cut --help‘ for more information.
./24p3.sh: line 5: [: ==: unary operator expected
./24p3.sh: line 5: username and groupname is different.: command not found
[root@little ~]#
[root@little ~]# vi +5 24p3.sh
[root@little ~]#
[root@little ~]# ./24p3.sh
username and group is same.
[root@little ~]#
linux-practice(23-24)