July 21, 2014 (shell Learning 1)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.