Shell Programming Basic syntax

Source: Internet
Author: User

When writing a shell script, the first line to add: #!/bin/bash, because Linux is not only bash a parser, there are other, they have some differences between the syntax, so it is best to add this sentence, tell the system to use this parser.

One. Shell variables

Unlike some programming languages, shell variables do not have to be "$" when assigning values to a shell variable, but with "$" when used or exported. Add two parentheses to the subtraction. There is a "$" outside the brackets, and the variables inside the parentheses can be used without "$". It should be noted that variable assignment, the use of variables can not have a space, otherwise it will be parsed into a command, error without this command.

Example:

Results such as:

Two. Shell variable expression

Example:

Results

Three. Shell Test judge test or []

It is important to note that the use of [] must have a space between each variable, and the left and right brackets also have a space, or error.

Results

Four. Shell conditional branching Structure statement

1. Single-branch judgment statement

Format: if condition; Then results fi, the final side must have fi, in the shell script, the control branch structure end must be opposite to the beginning of the word, for example, if <–> fi,case <–> Esac.

Results

2. Two-branch judgment statement

Results

3. Multi-Branch judgment statement

There are two kinds of multi-branch judgments, like the C language if else if,case. Just a few different forms.

Results


Results

Five. Shell Loop statements

1.while statements

The While statement executes the following statement whenever the condition is true.
Format:
While condition
Do
Statement
Done

It is important to note that the conditions in this case can be written in addition to while true, and all other conditions must be judged by test or [].


2.until statements

The until statement executes the following statement as long as the condition is false
Format:
Until conditions
Do
Statement
Done

Results

3.for statements

Format:
For variable in list
Do
Statement
Done

Results

Six. Shell functions

Format:
[function] FuncName ()
{
Statement
[Return value]
}
The return value is optional, and if no return is displayed, the result of the last statement execution is returned by default.

The Shell function return value can only be an integer, which is generally used to indicate success or failure of the function, 0 for success, and other values to fail. If you return other data, such as a string, you will often get an error message: "Numeric argument required".

If you have to make the function return a string, you can first define a variable to receive the result of the function, and the script accesses the variable when needed to get the function return value.

Function arguments from $ to $n,$0 are file names.

Example:

Results

return string, Error

Results

Shell Programming Basic syntax

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.