2017 the latest enterprise face Test shell (iii)

Source: Internet
Author: User

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)

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.