【shell 指令碼】刪除/var/log 下的日誌

楊奇龍發表於2011-03-13
開始學習shell 指令碼的編寫了,寫了一個刪除日誌的指令碼。
root@client.example.com # more test.sh   
#!/bin/bash
# cleanup /var/log/message
LOG_DIR=/var/log
ROOT_DID=0
LINES=50
E_XCD=66
E_NOTROOT=67

if [[ "$UID" -ne "$ROOT_UID" ]]
then
 echo "Must be root to run this script."
 exit $E_NOTROOT
fi

if [ -n "$1" ]
then
    lines=$1
else
    lines=$LINES
fi

# E_WRONGARGS =65
# case "$1" in
# ""      ) lines=50;;
# *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup ";;
# *       ) lines=$1;;
#  esac

#cd $LOG_DIR
#if [ "$PWD" !="$LOG_DIR" ]
# then
#    echo "Cant't change to $LOG_DIR."
#    exit $E_XCE
#fi
cd /var/log || {
    echo "Cant'change to necessary didrectory." >&2
    exit $E_XCD;
   }
tail -$lines messages > mesg.tmp
mv mesg.tmp messages
cat /dev/null > wtwp
echo "Logs cleaned up"

exit 0
"test.sh" 59L, 792C written
測試:
root@client.example.com # wc -l messages
43 messages
root@client.example.com # sh test.sh 20
Logs cleaned up
root@client.example.com # wc -l messages
20 messages
root@client.example.com #

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-689292/,如需轉載,請註明出處,否則將追究法律責任。

相關文章