Shell exercises (11)

Source: Internet
Author: User

Exercise 1:Statistics and summation

Requirements: calculates the number of digits that appear in each row in document 1.txt and calculates a total of several numbers in the entire document.

Reference Answer:

#!/bin/bash# date:2018 March 6 sum=0for i in ' cat/root/2.txt ' do line= ' Echo-n "$i" |sed ' s/[^0-9]//g ' |wc-c ' sum=$[$sum + $line]doneecho $sum

Exercise 2: count network card traffic

Requirements: write a script that detects your network traffic and logs it into a log. Need to follow the following format, and one minute statistics (only need to statistics external network card, assuming the network card name is eth0):

2017-08-04 01:11

Eth0 input:1000bps

Eth0 output:200000bps

-----------------------------

2017-08-04 01:12

Eth0 input:1000bps

Eth0 output:200000bps

Tip: Use sar-n DEV 1 59 to count the average network card traffic for one minute, just the last average. In addition, pay attention to the conversion, 1byt=8bit

Reference Answer:

#!/bin/bash# date:2018 March 6 while:d o d= ' date + "%F%T" ' logfile=/tmp/ens33.txt [-f $logfile] | | Touch $logfile Echo $d >> $logfile sar-n DEV 1 59|grep "Average Time" |grep "Ens33" |awk ' {print $ "input\t" $3*1000*8 " Bps\n "$" output\t "$4*1000*8" bps "} ' >> $logfile echo"------------------------------">> $logfiledone

Exercise 3: batch Kill process

requirement: scripting kills this script because the clearmem.sh script causes site access to become slow.

Reference Answer:

PS aux|grep clearmem.sh|grep-v Grep|awk ' {print $} ' |xargs Kill


Shell exercises (11)

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.