shell 清理脚本建议
您能否让我知道您对此脚本的想法以及您是否认为可以通过任何方法对其进行改进?
我正在尝试创建一个清理脚本,该脚本将通过 cron 作业在我们的 Linux 服务器上每周运行一次。
在脚本的一部分,我调用一个文本文件,其中包含可以从中删除的用户名列表,该文件的内容可能每周都会发生变化。
#!/bin/bash
DAY=$(date +"%d%b%Y")
HOME='/home/user'
DOCS='/var/program/alpha/top/is'
SCRATCH='/var/program/beta/top/_temp/'
USER='/home/user/deleteuserdata.txt'
DELUSER=$USER
cd $SCRATCH
rm -rf _temp-*/
cd $DOCS
while read DELUSER; do
find $DOCS/"$DELUSER"_info* -name "*.pdf" -size +1000k -exec rm {} \;
done < $USER > $HOME/"$DAY"dellogs.txt
Can you let me know your thoughts on this script and if you think it can be improved by any method?
I'm trying to create a clean up script that will run once a week by a cron job by root on our linux servers.
At one part of the script I call a text file that will have a list of user's names that can be deleted from, the contains of this file might change week to week.
#!/bin/bash
DAY=$(date +"%d%b%Y")
HOME='/home/user'
DOCS='/var/program/alpha/top/is'
SCRATCH='/var/program/beta/top/_temp/'
USER='/home/user/deleteuserdata.txt'
DELUSER=$USER
cd $SCRATCH
rm -rf _temp-*/
cd $DOCS
while read DELUSER; do
find $DOCS/"$DELUSER"_info* -name "*.pdf" -size +1000k -exec rm {} \;
done < $USER > $HOME/"$DAY"dellogs.txt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几乎所有地方都应该引用变量。优先选择 Pushd/popd 而不是 cd(更容易记住之前的路径)。可能更喜欢
find -delete
而不是 spawn-some-exec rm
。添加错误检查(bash -e)和 -x 以查看它在出现问题时退出的位置。You should quote variables almost everywhere. Prefer pushd/popd over cd (easier to remember pervious path). Probably want to prefer
find -delete
over the spawn-some-exec rm
. Add error checking (bash -e), and -x to see where it exits when it comes to that.