Shell scripts in Linux

Source: Internet
Author: User

▲ Shell Script

Shell Script is to use Shell the function of a program written by this program is to use a plain text file that will some Shell Grammar and instruction are written in, with regular expressions, piping commands and data flow redirection, to achieve our desired processing purposes.

Shell Script can be used to track and manage the important work of the system, simple intrusion detection function, the proposed data processing

?

in the Shell Script matters needing attention in the

    1. Multiple whitespace between instructions and parameters is ignored
    2. Blank Lines and Tab will also be ignored .
    3. If you read a Enter symbols (CR) , you try to start executing the command
    4. If there is too much content on a line, you can use \[enter] to extend to the next line
    5. any addition to # The following words, which are all treated as comment text, are ignored

?

How to perform . SH script File

    1. will be shell.sh plus the ability to read and Execute permissions to ./shell.sh to perform
    2. directly to SH shell.sh the way to do it directly. sh is a Soft connection to bash.

?

SH [-NVX] scripts.sh

???? Parameters - n???????? do not perform Script , only query syntax issues

???? Parameters - v???????? in the execution Script before you start, Script content output to the screen

???? Parameters - x???????? will be used to the Script content is displayed on the screen

?

Code to be added when scripting:

???? function, version information, author and contact method, copyright notice method, history, more special commands use absolute path to release, declare and set environment variables required for script runtime

?

For example, write a print Hello World script with the following code:

#!/bin/bash

#Program:

# This scripts was used to print Hello World

#History: 2015-1-26 youname first release

path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/ Bin

export PATH

?

echo "Hello World"

exit 0

?

▲test Judging

Judgment about a file name (presence or not)

???? TEST–[BCEDFPSL] FileName

About permission detection of files

???? TEST–[RWXUGKS] FileName

Comparison of two files

???? Test File1–[nt,ot,ef] File2

???????? -NT???? Newer than

???????? -ot???? Older than

???????? -EF???? File1 and file2 are the same files and can be used on hard link 's judgment

determination between two integers

???? Test N1–[eq,ne,gt,lt,ge,le] N2

???????? -eq???? Equal

???????? -ne???? Not equal

???????? -GT???? Greater than

???????? -LT???? Less than

???????? -ge???? Greater than or equal

???????? -le???? Less than or equal

Determining the data of a string

???? Test–z string???????? if string is empty (NULL) , it is true

???? Test–n string???????? if string is not empty; true

???? Test str1 = str2???????? if equal, the callback true

???? Test str1! = str2???? if not, the callback true

Multiple Criteria Determination

???? Parameters -A ( and , both of which were established simultaneously)

???? Parameters - o ( or , any one established)

???? Parameters ! (Reversed phase State)

?

Judging Symbols []

???? Placing the judging feature and the judged object in brackets, without requiring Test the

?

▲if Judgment Statement

???? If [ conditional judgment ]; Then???????? Fi End

???? If [ conditional judgment ]; Then else???? Fi End

???? If [ conditional judgment 1];then elif[ condition- 2];then Else???? Fi End

?

▲ Case...esac Judging

?

▲function function

The format is as follows:

???? function fname ()

???? {

???? Program Segment

}

?

function setting must be at the top of the program

has built-in variables, $ represents a function name, followed by a variable labeled $1,$2 ...

?

▲ Loop Control

while Loop: ???? when condition when established, the cycle begins

while [condition]

Do

???? Procedure paragraph

Done

?

until Loop: ???? when condition when established, terminates the loop

until [condition]

Do

???? Procedure paragraph

Done

?

▲for Loops

format 1 :

For ( initial value ; Limit Value ; Step Size ))

Do

???? Program Segment

Done

?

format 2 :

For Var in con1 con2 Con3 ...

Do

???? Program Segment

Done

Shell scripts in Linux

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.