Exercise 1:Batch Change file name
Requirements:
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
Reference Answer:
#!/bin/bash# date:2018 February 7 cd/root/123find. -type f-name "*.txt" >/tmp/txt.listfor f in ' cat/tmp/txt.list ' do mv $f $f. bakdoned= ' date + '%y%m%d%h%m%s ' TAR-ZCF $d. tar.gz *.bakfor F in ' cat/tmp/txt.list ' do mv $f. Bak $fdone
Of course you can also copy the TXT file, and then package the compression. But you have to delete it manually.
#!/bin/bash# date:2018 February 7 cd/root/123for txt in ' ls |grep-e '. txt$ "Do # echo" $txt "CP" $txt "/tmp/123/2>/dev/ Nulldonecd/tmp/123/find. -name "*.txt" |xargs-i mv {} {}.bakd= ' date + "%y%m%d%h%m%s" ' tar-zcf $d. tar.gz *.bak
Exercise 2: monitoring 80 ports
Requirements: write a script to determine whether the 80 port of the machine (if the service is httpd) is open, if the open does not do anything, if the discovery port does not exist, then restart the httpd service, the concurrent mail notify yourself. Once the script is written, it can be executed every minute, or it can write a dead loop script, 30s detection once
Reference Answer:
#!/bin/bash# date:2018 February 7 email= "[email protected]" while :d o result= ' netstat -lntp| grep ":80 " |grep listen| wc -l ' if [ $ result -eq "1" ];then continue else python /usr/local/sbin/mail.py "$email" " Check_80 " " The 80 port is down. " /usr/local/apache2.4/bin/apachectl restart >/dev/ Null 2>/dev/null n= ' Ps aux|grep httpd|grep -cv grep ' if [ $n -eq 0 ]; then /usr/local/apache2.4/bin/apachectl start 2>/tmp/apache_start.err fi if [ -s /tmp/apache_start.err ];then python /usr/local/sbin/ mail.py "$email" "Apache_start_error" "' Cat /tmp/apache_start.err '" fi fi sleep 30done
Mail Script mail.py:http://blog.51cto.com/11924224/2069732
Shell Exercises (iii)