Exercise 1:Statistics Log size
If we need to execute the script you wrote every hour. In the script to implement such a function, when the time is 0 and 12 points, you need to empty the file under directory/data/log/, notice can only empty the contents of the file and cannot delete the file. The other time just need to count the size of each file, a file line, output to a date and time as the name of the log. You need to consider the file
in sub-directories such as level Two, level three 、... in the/data/log/directory.
Reference Answer:
#!/bin/bash# date:2018 February 25 logdir= "/data/log/" t= ' date +%h ' d= ' date +%f-%h ' [-d/tmp/log_size] | | Mkdir/tmp/log_sizefor log in ' Find $logdir-type f ' do if [$t = = "0"] | | [$t = [] thentrue > $log elsedu-sh $log >>/tmp/log_size/$d fidone
Exercise 2: Statistics common commands
Requirements:
Write a shell script to see what commands you use the most, and list the commands you top10 most often.
Idea: We need to use a file is. bash_history, then go to sort, uniq, the rest will not need me to say more. Very simple a shell.
Reference Answer:
#!/bin/bash# date:2018 February 25 Sort/root/.bash_history|uniq-c|sort-rn|head
Exercise 3: getting a list of files
Requirements:
There is a server as a Web application, there is a directory (/data/web/attachment) is not timed to be uploaded by users of new files, but do not know when to upload. So, we need to do it every 5 minutes to detect if a new file is generated.
Please write a shell script to complete the detection. If a new file is available after detection, the list of new files needs to be output to a log that is divided into names by year, month, day, hour. Please do not think too complex, the core command only one find/data/web/attachment-mmin-5
Train of thought: every 5 minutes, that must have a scheduled task, every 5 minutes to perform. When the script detects, it uses the Find command to find the files that have been updated in 5 minutes, and if there is an update, the command will output something, otherwise there is no output. We can compare the number of rows of the output result to see if it is greater than 0
Reference Answer:
#!/bin/bash# date:2018 February 25 d= ' date-d "-5 min" +%y%m%d%h%m ' basedir=/data/web/attachmentfind $basedir-type f-mmin-5 &G T /tmp/newfile.txtn= ' wc-l/tmp/newfile.txt ' if [$n-gt 0];then/bin/mv/tmp/newfile.txt/tmp/$dfi
Shell Exercises (vii)