【发布时间】:2018-05-11 06:58:44
【问题描述】:
我正忙着写一个shell脚本;我被困住了。我正在尝试检查目录是否存在特定大小的违规行为;当它到达那里时;我想删除所述目录中的文件;基于+mtime。 话虽如此;我对 bash 很陌生,并且被困在算术部分。这是代码: #!/bin/bash
# Usage: cleanup_old_logs <folder> <days>
# Removes all log files in the directory older than a certain number of days
ALERT=90
FOLDER=~ NAME_TO_FOLDER_T)_MONITOR - removed for example
SIZE=$(df -k $FOLDER | tail -1 | awk '{print $4}')
N_DAYS=2
# check if the available space is smaller than < 90%
if (($SIZE -gt $ALERT)); then
# find all files under $FOLDER and delete them
echo "Deleting files in $FOLDER older than $N_DAYS days"
# Remove
find $FOLDER/* -maxdepth 1 -mtime +$N_DAYS -type f|wc -l ## -exec rm -rf {} \;
fi
这是我的错误:
line 13: ((: 86% -gt 90%: division by 0 (error token is "90%")
【问题讨论】:
-
我刚刚意识到我的问题不完整。有人可以向我简要解释一下我在算术中做错了什么吗?或许可以为我指明正确的方向?
-
当批处理文件变得过于复杂时,我建议使用 Powershell。它需要一些学习时间,但它比批处理文件更强大。
-
使用 sed 删除 SIZE 中的尾随 %。但请注意,它不是跟踪文件夹大小,而是特定挂载的百分比使用情况。您应该使用 du 来查找目录大小。
-
另外,您的脚本与您发布的错误不同步,脚本中的 ALERT 设置为 90,在错误中显示 90%。
-
I am trying to check a directory for a specific size breach为此,您必须使用du而不是整个挂载点df检查目录大小
标签: bash