Shell 练习题-01

发布于 2018-05-02  324 次阅读


01

题目:

按照 xxxx-xx-xx 格式每日生成一个文件,例如今天生成的文件为 2017-12-20.log,并且把磁盘使用状况写到这个文件中(写出具体命令即可,不需要 crond)

脚本:

#!/bin/bash

d=`date +%Y-%m-%d`

logfile=$d.log

df -h > $d.log

02

题目:

有日志 1.log,日志片段:

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/bootstrap.min.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/jquery.waypoints.min.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/jquery.stellar.min.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/hoverIntent.min.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/superfish.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:51 +0800] www.itwordsweb.com "/wp-content/themes/Kratos/js/kratos.js" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

116.30.197.28 - [02/May/2018:11:49:59 +0800] www.itwordsweb.com "/favicon.ico" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"

113.104.228.89 - [02/May/2018:11:50:11 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"

113.104.228.89 - [02/May/2018:11:50:11 +0800] www.itwordsweb.com "/favicon.ico" 200 "https://www.itwordsweb.com/" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"

222.88.91.50 - [02/May/2018:11:51:44 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

222.186.129.155 - [02/May/2018:11:51:44 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

122.228.199.114 - [02/May/2018:11:51:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

113.31.27.249 - [02/May/2018:11:51:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

61.130.28.141 - [02/May/2018:11:51:48 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

171.13.14.148 - [02/May/2018:11:53:01 +0800] www.itwordsweb.com "/283.html" 200 "https://www.itwordsweb.com/283.html" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)"

101.226.35.225 - [02/May/2018:12:01:23 +0800] 193.112.7.67 "//?feed=rss2" 301 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

101.226.35.225 - [02/May/2018:12:01:23 +0800] 193.112.7.67 "/feed" 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

222.88.91.50 - [02/May/2018:12:01:44 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

222.186.129.155 - [02/May/2018:12:01:44 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

122.228.199.114 - [02/May/2018:12:01:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

113.31.27.249 - [02/May/2018:12:01:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

61.130.28.141 - [02/May/2018:12:01:48 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

222.88.91.50 - [02/May/2018:12:11:44 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

222.186.129.155 - [02/May/2018:12:11:45 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

122.228.199.114 - [02/May/2018:12:11:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

113.31.27.249 - [02/May/2018:12:11:47 +0800] www.itwordsweb.com "/" 200 "-" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"

要求:统计出每个 IP 的访问量有多少?

解:awk 可以使用-F 指定分隔符,不做指定默认使用空格;sort -n 表示以数字的形式排序;uniq -c 是用来去重并计算重复的行数;最后再使用 sort -n 来做一下排序(要去重并计算重复的行数,必须要先排序)

脚本:

awk '{print $1}' 1.log | sort -n| uniq -c|sort -n

      1 171.13.14.148

      2 101.226.35.225

      2 113.104.228.89

      2 61.130.28.141

      3 113.31.27.249

      3 122.228.199.114

      3 222.186.129.155

      3 222.88.91.50

      7 116.30.197.28

03

题目:

写一个脚本计算一下 linux 系统所有进程占用内存大小的和。(提示,使用 ps 或者 top 命令)

脚本:

#!/bin/bash

sum=0

for mem in `ps -aux | awk '{print $6}'|grep -v 'RSS'`

do

    sum=$[$sum+$mem]

done

echo $sum