Bash脚本,用于检测文件的大小是否正确 [英] Bash script that detects if a file is the correct size

查看:93
本文介绍了Bash脚本,用于检测文件的大小是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让这个bash脚本调度并发送电子邮件,因为它检测到某个类型的文件(文件以.gz结尾),这是24小时前最后修改过的,在某个目录中低于某个大小,我目前在我的整台电脑上运行它。 (我把它设置为10千字节)。



它只是发送一个电子邮件,我希望它只发送一个,如果它实际检测到该文件是在门槛,如果有人可以通过它,并试图告诉我我搞砸了哪里会很棒!这是我的第一个严肃的脚本之一,所以简单的词汇!



1)脚本将找到以.gz结尾的目录中的所有文件在过去24小时内修改过的。



2)它会检查这些文件并确保它们的大小超过一定的阈值(例如10千字节)



3)如果它们什么都不做,但是如果它们低于该阈值,则会发送一封电子邮件到所述电子邮件地址



I'm trying to make this bash script dispatch and email when it detects that a certain type of file (files ending in ".gz"), which was last modified 24 hours ago, is below a certain size in a directory, I currently have it running on my entire computer. (I have it set to 10 kilobytes).

It just sends an email regardless, I expect it only to send one if it actually detects that the file is under the threshold, and if someone could please go through it and try to tell me where I messed it up that would be great! This is one of my first serious scripts so go easy on the vocabulary!

1) The script will find all of the files in a directory ending with ".gz" that were modified in the last 24 hours.

2) It will check these files and make sure they are above a certain threshold in size (say 10 kilobytes for example)

3) If they are it will do nothing, but if they are under that threshold, an email will be dispatched to the said email address

#!/bin/bash
for file in /*; do
stat $file
    FAILURE= find . -name "*.gz" -size -10k -mtime -1 -printf 'Failure %p\n'
done
if $FAILURE
then
    echo "The backup test has failed!" | mail -s "BACKUP FAILURE" myemail@gmail.com
fi

推荐答案

file
FAILURE = find。 -name * .gz -size -10k -mtime - 1 - printf ' 失败% p \ n'
完成
如果
file FAILURE= find . -name "*.gz" -size -10k -mtime -1 -printf 'Failure %p\n' done if

FAILURE
然后
echo 备份测试失败! | mail -s BACKUP FAILURE myemail@gmail.com
fi
FAILURE then echo "The backup test has failed!" | mail -s "BACKUP FAILURE" myemail@gmail.com fi


我写了任何bash脚本已经有一段时间但是我觉得你可能需要这样的东西:

It is some time since I wrote any bash scripts but I think you may need something like:
#!/bin/bash
# find all .gz files of 10k since yesterday, and count them
cd <directory where the files are stored>
COUNT = `find . -name "*.gz" -size -10k -mtime -1 -print | wc -l`
if [


这篇关于Bash脚本,用于检测文件的大小是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆