shell练习题-每日生成一个文件

题目来自:http://blog.lishiming.net/?p=31

要求:

请按照这样的日期格式(xxxx-xx-xx)每日在/test目录下生成一个文件(判断目录是否存在),例如今天生成的文件为 2018-6-11.log, 并且把磁盘的使用情况写到到这个文件中)

答案

[root@liang scritp]# cat logfile.sh 
#!/bin/bash
date=`date +%F`
dir=/test
if [ -d "$dir" ];then
        /usr/bin/df -h >${dir}/${date}.log
else
        /usr/bin/mkdir /test
        /usr/bin/df -h >${dir}/${date}.log
fi

定时任务

[root@liang scritp]# crontab -l
#Generate the df -h file every day
00 00 * * * /usr/bin/bash /scritp/logfile.sh


猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/80652683