2017 the latest enterprise face Test shell (iii)
Exercise 1: Write a shell script, similar to log cutting, the system has a logrotate program, you can 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
The script reads as follows:
#!/bin/sh
function LogDir ()
{
[-F $] && Rm-f $
}
For I in $ (SEQ 5-1 2)
Do
q=$[$i-1]
Logdir/data/1.log. $i
if [-f/data/1.log. $q]
Then
Mv/data/1.log. $q/data/1.log. $i
Fi
Done
Logdir/data/1.log.1
Mv/data/1.log/data/1.log.1
Exercise 2: Print a line with only one number
Title, a text document with only one number of lines to print out.
The answers are as follows:
[email protected] ~]# cat 2017-8-31.txt
My name is WTF
I Love ZD
Qqqqqqqqqqqqqqqqq
Aaaaaaaaaaaaaaaaa
Rrrrrrrrrr1tttttttttttttt
Yyyyyyyyyy3333ssssssssssss
4444444444444444444444444
4SRDTYGFJ
5
333
The script is as follows:
#!/bin/bash
File=/root/2017-8-31.txt
line=$ (wc-l $file |awk ' {print $} ')
For I in $ (seq 1 $line)
Do
Q= ' Sed-n "$i" P $file |grep-o ' [0-9] ' |wc-l '
If [$q-eq 1];then
Sed-n "$i" p $file
Fi
Done
Script Run Result:
Rrrrrrrrrr1tttttttttttttt
4SRDTYGFJ
5
Description
(1) Grep-o where-o means "only-matching", meaning "match only", more information: http://blog.csdn.net/u013982161/article/details/52334940
(2) Extract number: Wc-l $file |awk ' {print $} '
The explanations are as follows:
[Email protected] ~]# wc-l 2017-8-31.txt
Ten 2017-8-31.txt
[[email protected] ~]# wc-l 2017-8-31.txt |awk ' {print $} '
10
This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1961513
2017 the latest enterprise face Test shell (iii)