【问题标题】:One-liner for sending email if the number of files in a folder exceeds xx?如果文件夹中的文件数量超过 xx,则发送电子邮件的单线?
【发布时间】:2016-03-03 06:30:43
【问题描述】:

我想统计单个文件夹中的文件数量,如果文件数量超过一定数量(例如5000),则应发送电子邮件。

命令是这样的,但我想把它放在一行中(稍后用于 cron 作业):

count=$(find /dir1/dir2/dir3 -name "*.jpg" | wc -l) 
if [ $count -gt 5000 ]
then
    <command to send email>
else
    <do nothing>
fi

【问题讨论】:

  • 我使用瑞士德语的 ubunu 14.04。
  • @ThomasLandauer 的意思是“编程语言”。

标签: bash email count


【解决方案1】:

您可以将命令用半列分隔一行,并使用特殊引号 (``) 来检索输出的值(在这种情况下为wc -l):

if [ `find /dir1/dir2/dir3 -name "*.jpg" | wc -l` -gt 5000 ]; then <command to send email>; else <do nothing>; fi

如果你“什么都不做”真的什么都不是,那么你可以忽略它:

if [ `find /dir1/dir2/dir3 -name "*.jpg" | wc -l` -gt 5000 ]; then <command to send email>; fi

要发送邮件,您可以使用mail,例如:

echo "this is the body" | mail -s "this is the subject" "to@address"

检查此问题中的不同选项:How to send email from Terminal?。

【讨论】:

  • 感谢您的解决方案我已经用“;”尝试过在命令之间,但这没有用。也许我使用了错误的参数或字符“;”的位置不正确。我会再试一次...
猜你喜欢
  • 2023-03-06
  • 2020-01-25
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多