The utility scripts (a)

1. Write SHELL script, the file system usage exceeds 80% of the output directory

#!/bin/bash
df | sed 's/%//' | awk '$5>=80'

2. Use the sed command to add the string "hello world" after the first and third lines in the file file

(1)sed '1s/$/hello world/;3s/$/hello world/' x.txt
(2)sed '1a hello world;3a hello world' x.txt

3. Design a shell program, established 50 directory under / userdata directory that is user1-user50 and set permissions for each directory, the owner of the rights to: read-write execution, owning group permissions: read, execute, others: Read-only

#!/bin/bash
for i in {1..50}
do
	mkdir -p /userdata/user$i
	chmod 754 /userdata/user$i
done

4. Use the ping command to query a set of scripting IP address to detect whether they are in an active state requirements. (Range: 192.168.1.200-192.168.1.220, an IP ping packet transmission 4, ping a process can not be output to the terminal)

#!/bin/bash
for i in {200..220}
do	
	ping -c 4 -i 0.2 -W 1 192.168.1.$i &> /dev/null
	if [ $? -eq 0 ];then
		echo "192.168.1.$i is up"
	else	
		echo "192.168.1.$i is down"
	fi
done

5. Write SHELL script to transfer files larger than 100k under / usr / local / test directory to the / tmp directory

#!/bin/bash
find /usr/local/test -size +100k -exec cp {} /tmp/ \;

6. Query / var / log / secure log eight to nine log information

awk '$3>="08:00:00"&&$3<="09:00:00"' /var/log/secure

7. The system of statistics used bash as the total number of user login shell
a. Pretreatment assignment variables 0 = X
B. Then the progressive read / etc / passwd file, if found login shell is / bin / bash then add 1
c. after all of the processing is completed, the output values of x

#awk 'BEGIN{x=0}/bash$/{x++}END{print x}' /etc/passwd
(awk常用内置变量:		
	$0 文本当前行的全部内容		
	$1	文本的第1列		
	$2	文件的第2列		
	$3	文件的第3列,依此类推		
	NR	文件当前行的行号		
	NF	文件当前行的列数(有几列)
	)

8. formatted output / etc / passwd file
requirements: formatted output passwd file contents, list of behaviors required first title, in the middle of the printing user name UID home directory information, the last line of a total number of lines of text processing

awk -F: 'BEGIN{print "User\tUID\tHome"}\
{print $1 "\t" $3 "\t" $6}\
END{print "Total",NR,"lines."}' /etc/passwd

9. Statistics / etc / passwd file UID larger than the number of users 1000:

awk -F: '{if($3>1000){i++}}END{print i}' /etc/passwd

10. statistics / etc / passwd file Login Shell is "/ bin / bash" the number of users

awk -F: '{if($7~/bash$/){i++}}END{print i}' /etc/passwd
  1. Statistics are / etc / passwd file Login Shell is "/ bin / bash", login Shell is not "/ bin / bash" the number of users:
awk -F: '{if($7~/bash$/){i++}else{j++}} END{print i,j}' /etc/passwd
  1. AWK array
    syntax array (variable can store multiple values)
    format (1) to define arrays: an array name [index] = value element
    (2) call array format: name [index] array
    (3) traversing usage of the array: for (variable in the array name) {print array [variable name]}
    ############################### ##################################
    For chestnut: #awk 'BEGIN {a [0 ] = 11; A [. 1] = 88; Print A [. 1], A [0]} '(88. 11)
    #awk' the BEGIN {A ++; Print A} '(. 1)
    #awk' the BEGIN {A0 ++; Print A0} '(. 1 )
    #awk 'the BEGIN {A [0] ++; Print A [0]}'
    [SVR5 the root @ ~] # awk 'the BEGIN {A [0] = 0; A [. 1] =. 11; A [2] = 22; for (i in a) {print i, a [i]}} '
    Note that subscripts awk array using digital addition, using a string, the string requires double quotes:
    [SVR5 the root @ ~] awk # 'the BEGIN {A [ "Hehe"] =. 11; Print A [ "Hehe"]}'. 11
    ########################## #######################################
    13. problems:
    (1) a web log analysis traffic ranking, ask for IP, client visits, and ranked according to the number of visits
    (2) In analyzing the Web log files, each accesses the first column is the IP address of the client's record, where there will be a lot of duplicate IP addresses. So only use awk to extract this column is not enough, we also need to count the number of duplicate records and sorts.
    When extracting information through awk, using the IP address as the array index, when confronted with a repeat of this value is incremented array element 1, finally gained the number of IP addresses that appear. Text output can be used for sorting the sort command, related common option is -r, -n, -k. Where -n denotes ascending numerical order, reverse order and -r, -k can be specified on several fields to sort.

1) extracts the IP address and views

[root@svr5 ~]# awk '{ip[$1]++} \
>  END{for(i in ip) {print ip[i],i }}' /var/log/httpd/access_log
 4		127.0.0.14. 
17 		192.168.4.55. 
13		192.168.4.1106. .. ..

2) The results of the first) step according to traffic ranking

[root@svr5 ~]# awk  '{ip[$1]++} END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log | sort -nr
	17 192.168.4.5
	13 192.168.4.110
	4 127.0.0.1
	```


   






Guess you like

Origin blog.csdn.net/qq_44839276/article/details/90761321