(1) Please follow the date format (XXXX-XX-XX) to generate a daily file, such as the file generated today is 2017-07-05.log, and the use of the disk to write to this file, (do not consider Cron, just write the script can be)!
#! /bin/bashd= ' Date +%f ' logfile= $d. logdf-h > $logfile
(2) Statistics Log
There is log 1.log, the content is as follows: Log fragment:
112.111.12.248 - [25/sep/2013:16:08:31 +0800] formula-x.haotui.com "/seccode.php?update=0.5593110133088248" 200 "http://formula-x.haotui.com/ registerbbs.php " " mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; SV1;) "61.147.76.51 - [25/sep/2013:16:08:31 +0800]xyzdiy.5d6d.com "/attachment.php?aid=4554 &k=9ce51e2c376bc861603c7689d97c04a1&t=1334564048&fid=9&sid= Zgohwyolzq2qpw233zirsjiueu22xqe8f49jy9moursoe71 " 301" http://xyzdiy.***thread-1435-1-23.html " " mozilla/4.0 (COMPATIBLE; MSIE 6.0; WINDOWS NT 5.1; SV1; .NET CLR 1.1.4322; .net clr 2.0.50727) "
Requirement: What is the number of visits per IP?
# awk ' {print '} ' 1.log |sort-n|uniq-c
(3) Statistical Memory usage
Write a script that calculates the amount of memory that is consumed by all processes in the Linux system. (hint, use PS or TOP command)
#! /bin/bashsum=0for mem in ' ps aux |awk ' {print $6} ' |grep-v ' RSS ' does sum=$[$sum + $mem]doneecho "The total memory is $SU M "" K "
You can also use the awk command to calculate:
#ps aux | Grep-v ' RSS TTY ' |awk ' {(sum=sum+$6)}; End{print sum} '
(4) Design Monitoring Scripts
Monitor the surviving state of a remote machine (assuming IP 123.23.11.21) and send a message to yourself when you find a downtime. Tips:
You can use the ping command PING-C10 123.23.11.21
e-mail script can refer to https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py
Scripts can be turned into a dead loop, detected every 30s
#!/bin/baship=123.23.11.21[email protected]while 1do ping -c10 $ip >/dev/null 2>/dev/null if [ $? != "0" ] then python /usr/local/sbin/mail.py $ma $IP down $ip is down, Plese check. " #假设mail. Py has been written and set up fi sleep 30done
(5) Batch change file name
Find all files with the suffix. txt in the/123 directory
Bulk Modify. txt to. txt.bak
Package all. bak files into 123.tar.gz
The name of the batch restore file, that is, to remove the added. Bak again
#!/bin/bash# #查找txt文件find/123-type f-name "*.txt" >/tmp/txt.list# #批量修改文件名for F in ' cat/tmp/txt.list ' do MV $f $f. bakdone# #创建一个目录, in order to avoid the directory already exists, so add a complex suffix name d= ' date +%y%m%d%h%m%s ' mkdir/tmp/123_$d# #把. bak file to/tmp/123_$dfor F in ' CAT/ Tmp/txt.list ' do CP $f. bak/tmp/123_$ddone# #打包压缩cd/tmp/tar czf 123.tar.gz 123_$d/# #还原for F in ' cat/tmp/txt.list ' do MV $f. Bak $fdone
This article is from "a Man & A computer" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1951336
Shell exercises (1)