| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Cleanup, version 3 |
| 3 | |
| 4 | # Warning: |
| 5 | # ------- |
| 6 | # This script uses quite a number of features that will be explained |
| 7 | #+ later on. |
| 8 | # By the time you've finished the first half of the book, |
| 9 | #+ there should be nothing mysterious about it. |
| 10 | |
| 11 | |
| 12 | |
| 13 | LOG_DIR=/var/logscip-shell filesystem . . `/var/log`. |
| 14 | ROOT_UID=0 # Only users with $UID 0 have root privileges. |
| 15 | LINES=50 # Default number of lines saved. |
| 16 | E_XCD=86 # Can't change directory? |
| 17 | E_NOTROOT=87 # Non-root exit error. |
| 18 | |
| 19 | |
| 20 | # Run as root, of course. |
| 21 | if [scip-shell system . . `[`. "$UID" -ne "$ROOT_UID" ] |
| 22 | then |
| 23 | echoscip-shell system . . echo. "Must be root to run this script." |
| 24 | exit $E_NOTROOT |
| 25 | fi |
| 26 | |
| 27 | if [scip-shell system . . `[`. -n "$1" ] |
| 28 | # Test whether command-line argument is present (non-empty). |
| 29 | then |
| 30 | lines=$1 |
| 31 | else |
| 32 | lines=$LINES # Default, if not specified on command-line. |
| 33 | fi |
| 34 | |
| 35 | |
| 36 | # Stephane Chazelas suggests the following, |
| 37 | #+ as a better way of checking command-line arguments, |
| 38 | #+ but this is still a bit advanced for this stage of the tutorial. |
| 39 | # |
| 40 | # E_WRONGARGS=85 # Non-numerical argument (bad argument format). |
| 41 | # |
| 42 | # case "$1" in |
| 43 | # "" ) lines=50;; |
| 44 | # *[!0-9]*) echo "Usage: `basename $0` lines-to-cleanup"; |
| 45 | # exit $E_WRONGARGS;; |
| 46 | # * ) lines=$1;; |
| 47 | # esac |
| 48 | # |
| 49 | #* Skip ahead to "Loops" chapter to decipher all this. |
| 50 | |
| 51 | |
| 52 | cd $LOG_DIR |
| 53 | |
| 54 | if [scip-shell system . . `[`. `pwdscip-shell system . . pwd.` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ] |
| 55 | # Not in /var/log? |
| 56 | then |
| 57 | echoscip-shell system . . echo. "Can't change to $LOG_DIR." |
| 58 | exit $E_XCD |
| 59 | fi # Doublecheck if in right directory before messing with log file. |
| 60 | |
| 61 | # Far more efficient is: |
| 62 | # |
| 63 | # cd /var/log || { |
| 64 | # echo "Cannot change to necessary directory." >&2 |
| 65 | # exit $E_XCD; |
| 66 | # } |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | tailscip-shell system . . tail. -n $lines messages > mesg.temp # Save last section of message log file. |
| 72 | mvscip-shell system . . mv. mesg.temp messages # Rename it as system log file. |
| 73 | |
| 74 | |
| 75 | # cat /dev/null > messages |
| 76 | #* No longer needed, as the above method is safer. |
| 77 | |
| 78 | catscip-shell system . . cat. /dev/nullscip-shell filesystem . . `/dev/null`. > wtmp # ': > wtmp' and '> wtmp' have the same effect. |
| 79 | echoscip-shell system . . echo. "Log files cleaned up." |
| 80 | # Note that there are other log files in /var/log not affected |
| 81 | #+ by this script. |
| 82 | |
| 83 | exit 0 |
| 84 | # A zero return value from the script upon exit indicates success |
| 85 | #+ to the shell. |