Clear log files under/var/log
Basic edition:
1 # Cleanup 2 # Of course, use the root identity to run this script 3 4 cd/var/log 5 cat/dev/null> messages 6 cat/dev/null> wtmp 7 echo" logs cleaned up."
Version 2:
1 #! /Bin/bash 2 # correct start part of a Bash script. 3 4 # Cleanup, version 2 5 6 # Run as root. 7 # insert code here to print the error message and exit when it is not the root identity. 8 9 LOG_DIR =/var/log 10 # If a variable is used, it is better than writing the code. 11 cd $ LOG_DIR 12 13 cat/dev/null> messages 14 cat/dev/null> wtmp 15 16 17 echo "Logs cleaned up. "18 19 exit # This command is a correct and appropriate method to exit the script.
Version 3:
1 #! /Bin/bash 2 # Clear, version 3 3 4 # Warning: 5 # ------- 6 # This script has many features that are explained in the following sections, about 7 # Of the first half of the book, 8 # You will feel that it is nothing mysterious. 9 #10 11 12 13 LOG_DIR =/var/log 14 ROOT_UID = 0 # When the UID is 0, the user has the root user permission 15 LINES = 50 # The default number of LINES saved 16 E_XCD = 66 # The directory cannot be modified? 17 E_NOTROOT = 67 # non-root users will exit 18 19 20 with an error # Use the root user to run 21 if ["$ UID"-ne "$ ROOT_UID"] 22 then 23 echo "Must be root to run this script. "24 exit $ E_NOTROOT 25 fi 26 27 if [-n" $1 "] 28 # test whether a command line parameter exists (not empty ). 29 then 30 lines = $1 31 else 32 lines = $ LINES # default, if you do not specify 33 fi 34 35 36 # Stephen Chazelas in the command line, we recommend that you use the following 37 # + method to check the command line parameters. 38 # + but this chapter is still a little advanced. 39 #40 # E_WRONGARGS = 65 # non-numeric parameters (Incorrect Parameter format) 41 #4 2 # case "$1" in 43 # "") lines = 50; 44 #*[! 0-9] *) echo "Usage: 'basename $ 0' file-to-cleanup"; exit $ E_WRONGARGS; 45 # *) lines = $1 ;; 46 # esac 47 #48 # * The above content is not described in detail until the "Loops" chapter. 49 50 51 cd $ LOG_DIR 52 53 if ['pwd '! = "$ LOG_DIR"] # Or if ["$ PWD "! = "$ LOG_DIR"] 54 # Not in/var/log? 55 then 56 echo "Can't change to $ LOG_DIR. "57 exit $ E_XCD 58 fi # Check whether the current directory is correct before processing the log file. 59 60 # more efficient approach is 61 #62 # cd/var/log | {63 # echo "Cannot change to necessary directory. "> & 2 64 # exit $ E_XCD; 65 #} 66 67 68 69 70 tail-$ lines messages> mesg. temp # Save the last part of the log file message. 71. mv mesg. temp messages # change to the new log directory. 72 73 74 # cat/dev/null> messages 75 # * no longer needed. The above method is safer. 76 77 cat/dev/null> wtmp # ':> wtmp' and '> wtmp' play the same role 78 echo "Logs cleaned up. "79 80 exit 0 81 #0 is returned before exiting. 0 indicates success.