Shell习题100例

每日一文件

要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为2017-12-20.log,并且把磁盘的使用情况写到这个文件中,提示:date、df

[root@centos-04 tmp]# date
2018年 12月 26日 星期三 19:29:13 CST
[root@centos-04 tmp]# date +%Y
2018
[root@centos-04 tmp]# date +%y
18
[root@centos-04 tmp]# date +%d
26
[root@centos-04 tmp]# date +%m
12
[root@centos-04 tmp]# date +%H
19
[root@centos-04 tmp]# date +%M
30
[root@centos-04 tmp]# date +%S
52
[root@centos-04 tmp]# date +%s
1545823854
[root@centos-04 tmp]# date +%F
2018-12-26
[root@centos-04 tmp]# date +%T
19:31:04
[root@centos-04 tmp]# 
[root@centos-04 tmp]# date +%w
3
[root@centos-04 tmp]# date +%W
52
[root@centos-04 tmp]# 

昨天日期

[root@centos-04 tmp]# date -d "-1 day" +%F
2018-12-25
[root@centos-04 tmp]# 

上一小时

[root@centos-04 tmp]# date -d "-1 hours" +%T
18:34:54
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 1.sh
#!/bin/bash
d=`date +%F`
df -h > $d.log
~                

[root@centos-04 tmp]# sh 1.sh 
[root@centos-04 tmp]# ls
1.sh            ansible-ssh-192.168.242.130-22-root  ansible-ssh-192.168.242.133-22-root  lua_uwpzx3       tmp.SLBPtZ45L9
2018-12-26.log  ansible-ssh-192.168.242.131-22-root  elasticsearch.4Kw1U8qo               nginx_proxy_tmp
456.log         ansible-ssh-192.168.242.132-22-root  hsperfdata_root                      proxy.log
[root@centos-04 tmp]# 
[root@centos-04 tmp]# cat 2018-12-26.log 
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   18G  6.2G   12G   36% /
devtmpfs                 898M     0  898M    0% /dev
tmpfs                    910M     0  910M    0% /dev/shm
tmpfs                    910M   30M  881M    4% /run
tmpfs                    910M     0  910M    0% /sys/fs/cgroup
/dev/sda1                497M  167M  331M   34% /boot
tmpfs                    182M     0  182M    0% /run/user/0
overlay                   18G  6.2G   12G   36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged
shm                       64M     0   64M    0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm
overlay                   18G  6.2G   12G   36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged
overlay                   18G  6.2G   12G   36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged
shm                       64M     0   64M    0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm
shm                       64M     0   64M    0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm
[root@centos-04 tmp]#   

改进版

d=`date +%F`   #获取当前日期
dir=/data/logs/disklog  #指定日志文件生成的目录
if [ ! -d $dir ]  #判断如果没有目录创建
then
    mkdir -p $dir
fi
df -h > $dir/$d.log  #将硬盘信息写到日志
find $dir/ -mtime +365 |xargs rm  #删除一年之前的文件

统计IP访问量  

 awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r|less
[root@centos-04 tmp]# vim 2.sh
#!/bin/bash
awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r
[root@centos-04 tmp]# sh 2.sh 

  

  

  

猜你喜欢

转载自www.cnblogs.com/sunyujun/p/10197514.html