Instance 1:
Step 1: Use VI to create a file **. Sh (this is not introduced here)
#! /Bin/bash Note: This specifies the shell to explain the following command.
CD/var/log # Switch the directory to/var/log (# It is the annotator in shell)
CAT/dev/null> messages # Here, input and output redirection is used. Here, input redirection is used./dev/null is a device in Linux, which is commonly known as a non-low hole.
CAT/dev/null> wtmp
Echo "logs cleaned up." Here is a sentence.
Enhanced version:
#! /Bin/bash
# Cleanup 2, version
Log_dir =/var/log (Note: shell is a weak type variable that does not need to be declared)
CD $ log_dir
CAT/dev/null> messages
CAT/dev/null> wtmp
Echo "logs cleaned up ."
Exit
#! /Bin/bash
Log_dir =/var/log
Root_uid = 0
Lines = 50
E_xcd = 66 # Cannot modify the directory?
E_notroot = 67 # non-root users will exit with an error
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 # default, if not specified in the command line
Fi
Bbpengwang: determines whether the string $1 is null. If it is null, false is returned. If it is not null, true is returned.
CD $ log_dir
If ['pwd '! = "$ Log_dir"] # Or if ["$ PWD "! = "$ Log_dir"]
# Not in/var/log?
Then
Echo "can't change to $ log_dir ."
Exit $ e_xcd
FI # Check whether the current directory is correct before processing the log file.
Tail-$ lines messages> mesg. Temp
MV mesg. Temp messages
CAT/dev/null> wtmp # ':> wtmp' and '> wtmp' play the same role
Echo "logs cleaned up ."
Exit 0
#0 is returned before exiting. 0 indicates success.
My Chinese name: Wang Peng; English name: pengwang; contact info: 1352920044; QQ group:363356101
note that if [] If must be written with a space between square brackets, the expression must be separated by a space.