1. Display the default shell of root, fedora or User1 user on the current system;
#grep:-E: Use extended regular expression, egrep,^: Line start match, |: or #cut:-D: Specify delimiter,-F: Take the first few columns [[email protected] shell]# grep-e "^root|fedora|user1"/ etc/passwd | Cut-d:-f1,7root:/bin/bash
2. Find the line with a set of parentheses after a word in the/etc/rc.d/init.d/functions file, such as: Hello ();
#grep: [: Alpha:]: letter,?: Number of matches, 0 or 1 times #\<: Word first match, \>: Suffix match, you can also use \b substitution, can match the word head can also match the suffix [[email protected] shell]# grep "\ <[[:alpha:]]\+_\? [[:alpha:]]\+\> () "/etc/rc.d/init.d/functionscheckpid () {daemon () {Killproc () {Pidfileofproc () {Pidofproc () { Status () {echo_success () {
3. Use the echo command to output an absolute path, using grep to remove its base name;
#文件名 # to see the output directly with BaseName, is the last/subsequent content, of course, if the following/should be removed [[email protected] test]# Basename/etc/init.d/httpd/httpd#grep-o "\ <[^/]\+/\?$ ": Gets the last/trailing non-null character #cut-d '/'-F1: Gets the first byte content with/as delimiter [[email protected] shell]# echo"/etc/init.d/httpd/"| Grep-o "\<[^/]\+/\?$" | Cut-d '/'-f1httpd
Extension: Take out its path name
#路径名, see the effect directly with the dirname command [[email protected] test]# dirname/etc/init.d/httpd//etc/init.d#grep-o "^/.*[^/]\+": Remove the last useless/ #awk-F '/[^/]*$ ' {print $} ': Find path [[email protected] test]# echo/etc/init.d/httpd/| Grep-o "^/.*[^/]\+" | Awk-f '/[^/]*$ ' {print $} '/ETC/INIT.D
4, find out the results of the ifconfig command between 1-255 numbers;
1. Use grep to stitch strings
[Email protected] test]# Ifconfig | Egrep-o "\<[1-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]\> "002992192168108130
2. Numerical comparison
#!/bin/bash# first find the number of bits length, then sort it, go to nums=$ (ifconfig | grep-o "\<[0-9]\{1,3\}\>" | sort-n-u) #进入for循坏, The numbers are compared for 1<=num<=255, and if the conditions are met, output for num in $numsdo if [$num-ge 1-a $num-le 255];then Echo $num fidone# run Results [[email protected] shell]#./test4.sh124810296480111127128168192255
5, Challenge: Write a mode, can match the reasonable IP address;
I understand that a reasonable IP address may not be accurate, 1.0.0.0~255.255.255.254
1.grep Stitching String
#测试文件里放入一些测试数据 [[email protected] shell]# cat test.txt0.0.1.2 #超出范围198.98.2.1255.255.255.254172.168.1.2257.1.1.1 # Size out of range 2555.1.1.1 #长度超出范围233.1.2 # Less than one 172.168.1.2.1 #字节个数多一个255 .255.255.255 #需要剔除的数据 # Because the pattern is long, let's look at the first byte # divide IP into one bit length, two bit length, three bit length # 1-bit length: 1-9, matching is [1-9] #2位长度: 10~99, 10 digits is [1-9], single digit is [0-9] #3位长度: Divided into 100~199:1[0-9][0-9],200~249:2[0-4][0-9], 250~255:25[0-5] #然后用 | Stitching, is 1~255, the other three bytes are also written according to this rule the regular expression is, different bytes with. Connect, but note to escape, or else think it matches any single character # last grep -v Remove 255.255.255.255[[email protected] shell]# egrep "^ ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) $ " test.txt | grep -v " 255.255.255.255 " 198.98.2.1255.255.255.254172.168.1.2# a little tidying up, 234 in fact the matching pattern is the same, (. * *) match three times can [[Email protected] shell]# egrep "([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) (\. ( [0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) {3}) " test.txt | grep -v " 255.255.255.255 " 255.255.255.254172.168.1.2172.168.1.2.1
2. Numerical comparison
[[email protected] test]# cat sixth.sh#/bin/baship=$1# enters A For loop, which intercepts each byte of content as a delimiter, and puts it into the group for (I=1; i<=4;i++)) do array[$i]=$ (echo $ip | cut -d "." -f$i) echo ${array[$i]}done# Array Each byte comparison is within range if [ ${array[1]} -ge 1 ] && [ ${array[1]} -le 255 ];then if [ ${array[2]} -ge 0 ] && [ ${array[ 2]}&NBSP;-LE&NBSP;255&NBSP;];THEN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;[&NBSP;${ARRAY[3]} -ge 0 ] && [ ${array[3]} -le 255 ];then &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;[&NBSP;${ARRAY[4]}&NBSP;-GE&NBSP;0&NBSP;] && [ ${array[4]} -le 255 ];then echo $ip fi fi fifi# Running Results [[email protected] test]# ./sixth.sh 192.168.1.219216812192.168.1.2
6, Challenge: Write a pattern, can match out all the email address;
Email address rules
1) The full address of the mailbox consists of the username @ domain name
2) User name at least three characters, composed of letters, numbers, underscores, letters are not case-sensitive
3) The domain name should have at least one. A minimum of 2 characters, which may consist of letters or numbers.
#测试文件 [[email protected] shell]# cat Mail.txt[email protected][email protected][email protected] #有下划线, the results should appear [email Protected] #有两个. , the result should appear ABCDE #没有 @[email protected] #没有. Delimiter #[:alnum:]: Letters or numbers, [[: Alnum:]_]: Letters or numbers or underscores, {3,}: At least three times #\.: At least one. Delimiter #[[:aln Um:]]: Letters or numbers, {2,} at least two characters #\. [[: Alnum:]] {2,}:. Numbers or characters appear at least once [[email protected] shell]# egrep "[[: Alnum:]_]{3,}@[[:alnum:]]{2,} (\.[ [: Alnum:]] {2,}) {1,}$ "Mail.txt[email protected][email protected][email protected][email protected]
7. Find the main root of the/var directory, and belong to all files or directories of mail;
#find:-user: Match owner,-group: Match Group [[email protected] shell]# Find/var-user root-group mail/var/spool/mail/var/spool/mail/ Root
8, find the current system does not belong to the main or group of files;
#find:-nouser: No master,-nogroup: No group, #特别要注意这里的或者, well-known Morgan Law, non (p and q) = (non p) or (non-Q), non (p or q) = (non-P) and (non-Q) #输出结果时, because find is found at once The result of passing directly to the following command may result in an execution error, avoid the error with Xargs [[email protected] shell]# Find/-nouser-a-nogroup | Xargs ls-lrt/root/rpm/readmes/cs_cz:total 4-r--r--r--. 1 226 12201 3874 Mar 8 readme/root/rpm/readmes:total 64drwxr-xr-x. 2 226 12201 4096 Mar 8 zh_tw. Big5drwxr-xr-x. 2 226 12201 4096 Mar 8 zh_twdrwxr-xr-x. 2 226 12201 4096 Mar 8 ZH_CN
Further: Find files or directories that are not owned by the master or group on the current system and have been visited in the last 3 days;
#-atime: Access time, n = [n,n+1],-n:[0,n), +n:[n+1,∞) [[email protected] ~]# Find/-nouser-a-nogroup-a-atime-3find: '/proc/508 4/TASK/5084/FD/5 ': No such file or directoryfind: '/PROC/5084/TASK/5084/FDINFO/5 ': No such file or Directoryfind: '/PROC/5 084/FD/5 ': No such file or directoryfind: '/PROC/5084/FDINFO/5 ': No such file or Directory/root/rpm/licenses/root/rpm/rea Dmes/root/rpm/readmes/fr_fr
9, find all the users in/etc directory have write permission files;
#-perm: Permissions matching, MODE: Exact match,-mode: Each class of objects must have the specified permission,/mode: Any kind of object has the right to meet the specified permissions # Here is all users must have write permissions, write permissions to convert to 8 binary is 2, other permission bits do not need to consider [email Protected] ~]# find/etc-perm-222 | Xargs ls-lrtlrwxrwxrwx. 1 root root 2015/etc/x11/fontpath.d/liberation-fonts-/usr/share/fonts/liberationlrwxrwxrwx. 1 root root 2015/etc/init.d-rc.d/init.dlrwxrwxrwx. 1 root root 2015/etc/rc.d/rc6.d/k85messagebus. /init.d/messagebuslrwxrwxrwx. 1 root root 2015/etc/rc.d/rc5.d/s22messagebus. /init.d/messagebuslrwxrwxrwx. 1 root root 2015/etc/rc.d/rc4.d/s22messagebus. /init.d/messagebus
#-type: Matching by type f: Normal file d: catalog file, S: Socket file, L: Soft Connection file, p: Pipe file, B: Block device file, C: Character device file #-size: Match by file size, unit can be k,m,g ,n (n,n+1),-n:[0,n), +n:[n+1,∞) [[email protected] ~]# find /etc -type f -size +1m -ls134648 1976 -rw-r--r-- 1 root root 2020885 Jul 17 2015 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml140412 7124 -rw-r--r-- 1 root root 7292701 jul 17 2015 /etc/selinux/targeted/modules/active/policy.kern140687 7124 -rw-r--r-- 1 root root 7292701 jul 17 2015 /etc/selinux/targeted/policy/policy.24
11, find/etc/init.d/directory, all users have execute permission, and other users have write permission files;
#perm: By the right to match # have EXECUTE permission is 111, other users have Write permission 2, the other user's rights is 1+2=3, so the combination of permissions is 113[[email protected] ~]# find/etc/init.d-perm-113- ls130387 0 lrwxrwxrwx 1 root root one Jul 2015/etc/init.d-rc.d/init.d
12. Find files that do not belong to root, bin or hadoop in the/usr directory;
#注意 () plus escape character [[Email protected] ~]# find /usr -not \ ( -user root -a -user bin -a -user hadoop \) -ls657606 84 - rwxr-xr-x 1 root root 82192 nov 25 2013 /usr/libexec/wnck-applet665926 44 -rwxr-xr-x 1 root root 41312 nov 25 2013 /usr/libexec/gnome-clock-applet-mechanism656684 92 -rwxr-xr-x 1 root root 90352 aug 22 2010 /usr/libexec/ obex-client658381 24 -rwxr-xr-x 1 root root 23864 nov 11 2010 /usr/libexec/geoclue-localnet677225 48 -rwxr-xr-x 1 root root 46480 nov 11 2010 /usr/libexec/gdu-notification-daemon
13, to find at least one type of/etc/directory Users do not have write permission files;
#-perm -222: Each type of user has write permissions #-not -perm -222: At least one class of users does not have write access [[Email protected] test]# find /etc -not -perm -222 -ls785634 20 -rwxr-xr-x 1 root root 19688 nov 22 2013 /etc/rc.d/rc.sysinit782928 4 drwxr-xr-x 2 root root 4096 Aug 24 12:12 /etc/rc.d/rc1.d915527 4 drwxr-xr-x 2 root root 4096 Aug 24 12:12 /etc/rc.d/rc3.d915533 4 drwxr-xr-x 2 root root 4096 aug 24 12:12 /etc/rc.d/rc4.d783245 4 -rw-r--r-- 1 root root 1962 Feb 17 2012 /etc/vimrc783239 4 -rw-r--r-- 1 root root 2620 Aug 16 2010 /etc/mtools.conf
14. Find files whose contents have been modified and not rooted or Hadoop for the last week in/etc directory;
[Email protected] test]# find/etc-mtime-7-a-not-user root-a-not-user Hadoop or: [[email protected] test]# Find/et C-mtime-7-a-not \ (-user root-o-user hadoop \)
If there is a mistake, please crossing comments, the small woman is greatly appreciated.
Mago Viban Fifth Week assignment