Shell exercises (13)

Source: Internet
Author: User

Exercise 1:Formatted output

Requirements: Enter a string of random numbers and then output by thousand. for example, the input number string is "123456789", the output is 123,456,789

#/bin/bash# date:2018 March 15 read-p "Please input a long number:" Numv= ' echo $num |sed ' s/[0-9]//g ' if [-N ' $v "]then EC    Ho "Please input a number." Exit 1fiecho $num |sed-r ' {: number;s/([0-9]+) ([0-9]{3})/\1,\2/;t number} '

Exercise 2: finding the active IP

Requirements: Write a shell script that lists the IP of the 192.168.0.0/24 network segment online. idea: For Loop, 0.1-0.254 go to ping in turn, can pass the description online.

Reference Answer:

#/bin/bash# date:2018 March 15 ips= "192.168.139." For i in ' SEQ 1 254 ' do ping-c 2 $ips $i >/dev/null 2>/dev/null if [$? = = "0"] then echo "$ips $i I    s online "Else echo" $ips $i is not online. " Fidone

Exercise 3: log archiving

Requirements: similar to log cutting, the system has a logrotate program to complete the archive. But now we're going to write ourselves a shell script to implement the archive.

Example: If the output log of the service is 1.log, I want to archive one every day, 1.log the next day to become 1.log.1, the third day 1.log.2, fourth day 1.log.3 until 1.log.5

reference answer:

#/bin/bash# date:2018 March 15 function lf () {[-F $]&& rm-f $1}for i in ' seq 5-1 2 ' do i2=$[$i-1] Lf/dat A/1.log. $i if [-f/data/1.log. $i 2] then Mv/data/1.log. $i 2/data/1.log. $i fidonelf/data/1.log.1mv/data /1.log/data/1.log.1


Shell exercises (13)

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.