Shell Exercises (iii)

Source: Internet
Author: User

Exercise 1:Batch Change file name

Requirements:

    1. Find all files with the suffix. txt in the/123 directory

    2. Bulk Modify. txt to. txt.bak

    3. Package all. bak files into 123.tar.gz

    4. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.