# 自动化任务示例脚本: # -更新本地文件数据库: echo -e "\e[4;32mUPDATING LOCAL FILE DATABASE\e[0m" updatedb if [ $? == 0 ]; then echo"The local file database was updated correctly." else echo"The local file database was not updated correctly." fi echo""
# -查找 和/或 删除有 777 权限的文件。 echo -e "\e[4;32mLOOKING FOR FILES WITH 777 PERMISSIONS\e[0m" # Enable either option (comment out the other line), but not both. # Option 1: Delete files without prompting for confirmation. Assumes GNU version of find. #find -type f -perm 0777 -delete # Option 2: Ask for confirmation before deleting files. More portable across systems. find -type f -perm 0777 -execrm -i {} +; echo"" # -文件系统使用率超过定义的阀值时发出警告 echo -e "\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m" THRESHOLD=30 whileread line; do # This variable stores the file system path as a string FILESYSTEM=$(echo$line | awk '{print $1}') # This variable stores the use percentage (XX%) PERCENTAGE=$(echo$line | awk '{print $5}') # Use percentage without the % sign. USAGE=${PERCENTAGE%?} if [ $USAGE -gt $THRESHOLD ]; then echo"The remaining available space in $FILESYSTEM is critically low. Used: $PERCENTAGE" fi done < <(df -h --total | grep -vi filesystem)
请注意该脚本最后一行两个 < 符号之间有个空格。
查找 777 权限文件的 Shell 脚本
使用 Cron
想更进一步提高效率,你不会想只是坐在你的电脑前手动执行这些脚本。相反,你会使用 cron 来调度这些任务周期性地执行,并把结果通过邮件发动给预先指定的接收者,或者将它们保存到使用 web 浏览器可以查看的文件中。
下面的脚本(filesystem_usage.sh)会运行有名的 df -h 命令,格式化输出到 HTML 表格并保存到 report.html 文件中: