LDP/LDP/guide/docbook/abs-guide/ex2.sh

86 lines
2.0 KiB
Bash
Raw Permalink Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2004-04-28 12:08:07 +00:00
# Cleanup, version 3
# Warning:
# -------
# This script uses quite a number of features that will be explained
#+ later on.
# By the time you've finished the first half of the book,
#+ there should be nothing mysterious about it.
2001-07-10 14:25:50 +00:00
LOG_DIR=/var/log
2001-09-04 13:27:31 +00:00
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
2008-11-23 22:43:47 +00:00
E_XCD=86 # Can't change directory?
E_NOTROOT=87 # Non-root exit error.
2001-07-10 14:25:50 +00:00
2004-04-28 12:08:07 +00:00
# Run as root, of course.
2001-07-10 14:25:50 +00:00
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
2001-09-04 13:27:31 +00:00
exit $E_NOTROOT
2001-07-10 14:25:50 +00:00
fi
if [ -n "$1" ]
2008-11-23 22:43:47 +00:00
# Test whether command-line argument is present (non-empty).
2001-07-10 14:25:50 +00:00
then
lines=$1
else
2008-11-23 22:43:47 +00:00
lines=$LINES # Default, if not specified on command-line.
2001-07-10 14:25:50 +00:00
fi
2001-09-04 13:27:31 +00:00
# Stephane Chazelas suggests the following,
2008-11-23 22:43:47 +00:00
#+ as a better way of checking command-line arguments,
2001-09-04 13:27:31 +00:00
#+ but this is still a bit advanced for this stage of the tutorial.
2001-07-10 14:25:50 +00:00
#
2008-11-23 22:43:47 +00:00
# E_WRONGARGS=85 # Non-numerical argument (bad argument format).
2001-07-10 14:25:50 +00:00
#
# case "$1" in
# "" ) lines=50;;
2012-11-27 14:56:18 +00:00
# *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup";
# exit $E_WRONGARGS;;
2001-07-10 14:25:50 +00:00
# * ) lines=$1;;
# esac
#
2002-06-17 13:17:07 +00:00
#* Skip ahead to "Loops" chapter to decipher all this.
2001-07-10 14:25:50 +00:00
cd $LOG_DIR
2002-04-01 16:04:17 +00:00
if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
2001-09-04 13:27:31 +00:00
# Not in /var/log?
2001-07-10 14:25:50 +00:00
then
echo "Can't change to $LOG_DIR."
2001-09-04 13:27:31 +00:00
exit $E_XCD
2008-11-23 22:43:47 +00:00
fi # Doublecheck if in right directory before messing with log file.
2001-07-10 14:25:50 +00:00
2008-11-23 22:43:47 +00:00
# Far more efficient is:
2002-06-17 13:17:07 +00:00
#
2001-07-10 14:25:50 +00:00
# cd /var/log || {
# echo "Cannot change to necessary directory." >&2
2001-09-04 13:27:31 +00:00
# exit $E_XCD;
2001-07-10 14:25:50 +00:00
# }
2008-11-23 22:43:47 +00:00
tail -n $lines messages > mesg.temp # Save last section of message log file.
2012-04-04 22:51:18 +00:00
mv mesg.temp messages # Rename it as system log file.
2001-07-10 14:25:50 +00:00
2008-11-23 22:43:47 +00:00
# cat /dev/null > messages
2001-09-04 13:27:31 +00:00
#* No longer needed, as the above method is safer.
2001-07-10 14:25:50 +00:00
2002-06-17 13:17:07 +00:00
cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect.
2011-05-03 15:38:48 +00:00
echo "Log files cleaned up."
# Note that there are other log files in /var/log not affected
#+ by this script.
2001-07-10 14:25:50 +00:00
exit 0
2008-11-23 22:43:47 +00:00
# A zero return value from the script upon exit indicates success
#+ to the shell.