shell编程学习(一)--初步入门

一、简单日志清除脚本

[root@shell scripts]# cat clean_messages.sh 
#!/bin/bash
# 清除日志脚本

LOG_DIR=/var/log
ROOT_UID=0  

#必须是root用户才能执行
if [ "$UID" -ne "$ROOT_UID" ]
  then
    echo "Must be root to run this scripts"
    exit 1;
fi

#切换目录不成功,给与提示并终止程序
cd $LOG_DIR || {
    echo "Cannot change to necessary directory."
    exit 1
}

cat /dev/null > messages && {
    echo "Logs cleaned up"
    exit 0
}

echo "Logs cleaned up fail."
exit 1

猜你喜欢

转载自www.cnblogs.com/hujinzhong/p/12705166.html