bash 脚本解析 du -s * 输出并警告超出特定存储限制的用户
我正在尝试编写一个脚本来检查主文件夹磁盘使用情况,并在用户高于 xxGB 时发出警告,通过电子邮件,
我将 du -s * 的输出转储到临时文件中,逐行读取它,当我尝试从 du 的输出中读取文件夹的大小和名称,它无法正常工作,只需执行 echo $file 我将每一行转储为两行,我尝试展开以用空格替换制表符,但也不起作用,我也不知道如何根据大小进行比较。
#!/bin/bash
#echo "Disk usage report for /homes on `hostname`"
EMAIL="[email protected]"
##########################
# check staff
#########################
cd /homes/staff/
file1="/root/scripts/temp_check"
file2="/root/scripts/temp_check2"
du -s * | sort -rn | head -15 |awk '{print}' > $file1
expand $file1 > $file2
for line in $(cat $file2)
do
echo $line
# echo $line | awk '{ print $2 }'
mail -s "Disk usage report for your homefolder" $EMAIL
done
I am trying to wrote a script to check homefolders disk usage, and warn users when they are above xxGB, by email
I dump the output of the du -s * to a temp file, read it line by line, and when i try to read the size and name of the folder from the output of du it does not work correctly, just doing an echo $file i get each line dumped as two lines, i tried expand to replace the tabs with spaces, but also didn't work, and I am not sure how to do the comaprison based on size too.
#!/bin/bash
#echo "Disk usage report for /homes on `hostname`"
EMAIL="[email protected]"
##########################
# check staff
#########################
cd /homes/staff/
file1="/root/scripts/temp_check"
file2="/root/scripts/temp_check2"
du -s * | sort -rn | head -15 |awk '{print}' > $file1
expand $file1 > $file2
for line in $(cat $file2)
do
echo $line
# echo $line | awk '{ print $2 }'
mail -s "Disk usage report for your homefolder" $EMAIL
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不简单地实施磁盘配额呢?几乎所有 Unix/Linux 系统都可以做到这一点。
然而,如果你真的想这样做,为什么还要搞这些阴谋呢?
du - s * 将生成两列输出,其中包含使用的磁盘空间和用户名。使用 while 循环而不是将所有内容放入临时文件中。
Why not simply implement disk quotas? Almost all Unix/Linux systems can do that.
However, if you're really want to do it this way, why all the machinations?
The
du - s *
will produce a two column output with diskspace used and user name. Use a while loop instead of putting everything in temporary files.for 循环根据空格标记您的输入。所以每个单词都变成了 $line。
您可以使用 while 循环来正确捕获输入,而不是 for 循环,例如
(您可以添加 暂时将 -x 设置为您的脚本以查看发生了什么)
The for loop is tokenizing your input based on spaces. So each word becomes a $line.
Instead of for loop, you can use a while loop to capture the input correctly, e.g.
(You could add set -x to your script temporarily to see what's happening)
看看 durep
安装Ubuntu 中的 durep
使用以下命令安装 durep
使用 durep
语法大致为
durep [OPTION]... [DIRECTORY]
“durep -w ~/durepweb -td 2″
<块引用>
这会将当前目录开始到深度 2 的目录树打印到控制台,并在目录 ~/durepweb 中创建网页(该目录必须存在)。
Have a look at durep
Install durep in Ubuntu
Use the following command to install durep
Using durep
Syntax is roughly
durep [OPTION]… [DIRECTORY]
“durep -w ~/durepweb -td 2″
如果您要求,
您将获得所有文件(隐藏文件除外)和目录的一一摘要,因为 * 是由 shell 扩展的。
将为您提供一行,总结所有内容,包括隐藏文件。
也会总结整个目录 - 并将包括隐藏文件(刚刚测试过)。
由于它只是一行,所以整行从 折叠
到
,因为您不需要对单行进行排序,将其减少到 15 行,并使用 awk 语句重复它。
If you ask for
you get a summary for all files (except hidden files) and directories one by one, because the * is expanded by the shell.
will give you a single line, everything summed up, including hidden files.
would summarize the whole directory too - and would include hidden files (just tested it).
Since it will just be one line, the whole line collapses from
to
because you don't need to sort a single line, reduce it to 15 lines, and repeat it with the awk-statement.