shell 清理脚本建议

发布于 2024-12-21 16:51:23 字数 541 浏览 0 评论 0原文

您能否让我知道您对此脚本的想法以及您是否认为可以通过任何方法对其进行改进?

我正在尝试创建一个清理脚本,该脚本将通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-12-28 16:51:23

几乎所有地方都应该引用变量。优先选择 Pushd/popd 而不是 cd(更容易记住之前的路径)。可能更喜欢 find -delete 而不是 spawn-some -exec rm。添加错误检查(bash -e)和 -x 以查看它在出现问题时退出的位置。

#!/bin/bash -ex

DELUSER="$USER" # setting this is useless because it's overriden in the while loop
pushd "$SCRATCH"
rm -Rf _temp-*/ || :
pushd "$DOCS"
while read DELUSER; do
    find "$DOCS/$DELUSER"_info* -name "*.pdf" -size +1000k -print -delete
done <"$USER" >"$HOME/${DAY}dellogs.txt"
popd
popd

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.

#!/bin/bash -ex

DELUSER="$USER" # setting this is useless because it's overriden in the while loop
pushd "$SCRATCH"
rm -Rf _temp-*/ || :
pushd "$DOCS"
while read DELUSER; do
    find "$DOCS/$DELUSER"_info* -name "*.pdf" -size +1000k -print -delete
done <"$USER" >"$HOME/${DAY}dellogs.txt"
popd
popd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文