【问题标题】:Cat if one new file are generated [closed]如果生成一个新文件,则 Cat [关闭]
【发布时间】:2019-07-12 12:21:32
【问题描述】:

我有一个脚本,如果有新电子邮件(使用 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

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

有什么简单的方法吗?

【问题讨论】:

  • 我已经测试了目录脚本的监视器,可能更改 then 部分可以使 cat 到新文件以应用格式更改。 filesindir=ls -t /Mail/new for i in $filesindir do echo "$listOF" | grep $i if [[ "$?" -eq "0" ]] 然后 listOF=$listOF" "$i fi 完成
  • 别担心。开始使用 Stack Overflow 时,每个人都会感到有些厌烦。 MCVE's 非常重要。

标签: linux scripting monitor


【解决方案1】:

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

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/

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2012-12-09
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多