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)