如果生成一个新文件,则为猫

时间:2019-07-12 12:21:32

标签: linux scripting monitor

我有一个脚本,如果收到一封新电子邮件(使用fetchmail-procmail),则会生成一个新文件,新邮件会显示在一个普通文件中,如果有一封新邮件,我需要处理该邮件的内容。 fetchmail日志示例:

procmail: [7709] Fri Jul 12 09:10:19 2019
procmail: Assigning 
"LASTFOLDER=Mail/new/1562933419.7709_0.localhost.localdomain"
Folder: Mail/new/1562933419.7709_0.localhost.localdomain 38398

仅当有一封新邮件时,我才需要检索此信息,并对其进行处理以更改原始接收文件的格式,然后将其复制到其他目录中,然后再清理该原始文件。

有一些简单的方法吗?

1 个答案:

答案 0 :(得分:0)

inotifywait (inotify工具的一部分)是实现目标的工具:

https://linux.die.net/man/1/inotifywait

这是一个示例脚本:

#!/bin/sh
MONITORDIR="/path/to/the/dir/to/monitor/"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
    mv NEWFILE /path/to/newdir

更多示例在这里:

https://techarena51.com/blog/inotify-tools-example/