shell脚本清空日志文件,应用场景:保留文件、清空内容 分析: 日志文件:/var/log/messages 只有root用户可以查看日志文件,普通用户不能。 shell代码: #!/bin/bash LOG_DIR=/var/log ROOT_UID=0 if [ "$UID" -ne "$ROOT_UID" ];then echo "Must be root to do it" exit 1 fi cd $LOG_DIR || { echo "The file $LOG_DIR is not exist" exit 1 } cat /dev/null > messages && echo "The Log is cleaned up..." exit 0