开始学习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 #