Shell Script Simple Example (i)

Source: Internet
Author: User

#!/bin/bash

# above is the shell script interpreter, which must be written at the beginning to represent the shell script. /bin/sh is more used under UNIX and features less than Bash. Use/bin/bash more, but/bin/sh is/bin/bash soft link in Linux.

# Cleanup, version 3

Log_dir=/var/log # This defines variables, which generally define frequently used content, and reference variables are more professional and convenient.

Root_uid=0 # Only the root user has permission. The $UID of root is 0.

LINES=50 # How many rows are saved.

E_XCD=86 # cannot switch directories, resulting in an exit error code.

E_NOROOT=87 # is not the root user and does not have permission to exit the error code.


If ["$UID"-ne "$ROOT _uid"] # If the current user is not ROOT. -ne means not equal to,-eq means equals.

Then

echo "must is root to run this script." # then Print.

Exit $E _noroot # and exit.

Fi


If [-N "$"] # if the first argument (after the script) is not nonempty. n denotes Non-zero.

Then

LINES=$1 # Assigns the value of the first parameter to the variable lines.

Else

lines= $LINES # If the first argument is empty, the lines value is the value given by the variable lines, which is 50.

Fi


# # # Above This paragraph can also replace # # #

#######################################################################

E_WRONGARGS=85 # Non-numeric exit code.

Case "$" in

"") lines= $LINES;; # If the first argument is empty, then lines is 50.

*[!0-9]*) echo "Usage ' basename $ ' lines-to-cleanup '";        Exit $E _wrongargs;; # can also be written as *[^0-9]*. is a wildcard syntax in which there is a non-numeric string, and the entire string is a non-pure number. [[:d Igit:]] Also represents a number, but only one number. ' BaseName $ ' is the script's own name (without the path). If the first parameter is entered as a non-pure number, the error code is printed.

*) lines=$1;; # If the first parameter is a pure number, then lines is the parameter value.

Esac

#######################################################################


CD $LOG _dir


If [' pwd '! = "$LOG _dir"] # Here's ' pwd ' can also be written as $PWD.! = means not equal or can be used with-ne.

Then

echo "Can ' t change to $LOG _dir."

Exit $E _xcd

Fi


# # # The above code can also be written # # #

#######################################################################

CD $LOG _dir | | {

echo "Cant" to necessary directory. ">&2 # >&2, which is 1>&2, redirects the standard output to a standard error. But it's all the same here. Indicates that the previous command execution failed after the execution of the following command. If the previous command executes successfully, the subsequent command does not execute.

Exit $E _xcd;

}

#######################################################################


Tail-n $lines Messages > Mesg.tmp

MV Mesg.temp messages


Cat/dev/null > Wtmp # Empty wtmp,: > Wtmp and > Wtmp are the same effect.


echo "Log files is cleaned up."


Exit 0 # Indicates that all is done and the final output status code 0 indicates success.

This article is from the "Alex Blog" blog, make sure to keep this source http://houjun19830610.blog.51cto.com/9269760/1786201

Shell Script Simple Example (i)

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.