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)