Shell Script Programming Basics

Source: Internet
Author: User
Tags case statement

The shell is an interpretive programming language.


The execution of an interpreted script file:

When an interpreted script file is executed, the script file is committed to the kernel. The kernel sees the first line of "#! "#! is called when the "followed by an interpreter, initiates an interpreter process and puts the file in subsequent

The interpreter is passed to the CPU for execution. After the script is finished executing, the interpretation process will end.


Shell Script Writing Specification:

First line: #!/bin/bash must be fixed to write

#author: movekj Script author

#date: Date Written

#version: 0.0.1 Version number

#description: Bash scription script feature description


Shell scripting is a procedural programming language with the following three execution processes:

Sequential execution: Executes backward from the first statement of a file

Select execution: Selective execution according to the given conditions

Loop execution: Repeating execution of statements in a loop body


Variable:

Local variables: Valid only for the current process

Var_name=value

Environment variables: Valid for the current process and its self-process

Export Var_name=value

Local variables: Valid in code blocks (typically used in functions)

Local Var_name=value


Position variables: $, $, $ ... $n

Special variables:

$#: Indicates the number of script parameters

$?: Indicates the execution status of the previous command (0 means execution succeeded, non-zero means execution failed)

$*: Represents a list of parameters for a script

[Email protected]: A list of parameters representing the script

Variable assignment:

Var_name=value

Declare [option] Var_name=value


Common conformance and meaning in shell scripts:

' Command ' (anti-apostrophe): Indicates the command reference, the execution result of the reference command

"" (double quotes): A weak reference, where there are $ plus variable names, will do variable substitution

' (single quote): A strong reference, where there are $ plus variable names, does not do variable substitution


Looping statements: (There are three common types of the following)

For loop: Iterates through the list, iterating to the last parameter in the list, exiting the loop.

For Var_name in LIST

Do

Loop body

Done

How the list is generated:

1. Seq Command

SEQ [star] [stepping] [end]

2, {...}

3. Directly type parameters separated by one or more spaces


While loop: Exits the loop when the condition does not satisfy the condition.

While condition

Do

Loop body

Done

Until loop: Exits the loop when the condition satisfies condition.

While condition

Do

Loop body

Done

SELECT statement:

If statement:

Single branch:

If COMMAND

Then

Select Branch

Fi

Dual Branch:

If COMMAND
Then

Select branch 1

Else

Select branch 2

Fi

Multi-branch:

If COMMAND

Then

Select branch 1

Elif COMMAND

Then

Select branch 2

Elif COMMAND
Then

Select Branch 3

...

Else

Select Branch N

Fi

Case statement:

Case Var_name in

)

;;

)

;;

)

;;

Esac


Shell condition Test

Character comparisons:

>, <, >=, <=, = =,! =

-Z: Test whether the variable is empty

-N: Test variable is not empty

Number comparison:

-GT,-lt,-ge,-le,-eq,-ne

File test:


Arithmetic operations

Operation in accordance with: + 、-、 *, * * (Powers),/,% (modulo/take-over)

Let var_name=expr

Let i++-let i= $I +1

Let i+=2-let i= $I +2

Let i-=2-i= $I-2

Other in accordance with and etc.


Expr AGR1 + AGR2 ...

$[EXPR]

$ ((EXPR))

Expr AGR1 + AGR2 ...










This article is from "Chalet Technology" blog, please make sure to keep this source http://starli.blog.51cto.com/8813574/1658097

Shell Script Programming Basics

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.