--shell Programming for Linux Programming (chapter II)

Source: Internet
Author: User
Tags arithmetic

This article describes the syntax of the shell, including variables in the shell, conditional judgments, control structures, and functions. In addition, all commands in this article are in variable condition control structure function. The next article is the execution of commands and commands.

The syntax of the shell

Conditions

all programming languages are based on testing the conditions and taking different actions based on different test results .。 One shell scripts are capable ofAny command that can be invoked from the command line. exit code to test, which also includes scripts written by yourself. That's what you're going to do with all the scripts you write. important reason for the end to include a command that returns a value
Test or [command
Most scripting programs use the Shell's Boolean judgment command [or test
For example: [-F FRED.C] Note must be [Symbol Leave a space between the conditions and the condition being checked。 If you put then and if on the same line, you need to separate the test statement and then with a semicolon.
The test command can be used with a condition type that can be divided into 3 classes: string comparisons, arithmetic comparisons, and file-related condition tests.
string comparison Results
string1 = string2
String1! = string2
-N String if the string is not empty, the result is true
-Z String if the string is null, the structure is true

Arithmetic comparison results

Expres1-ep Expres2 If two expressions are equal, the result is true
Expres1-ne Expres2 If two expressions are not equal, the result is true
EXPRES1-GT Expres2 If Expres1 is greater than expres2, the result is true
Expres1-ge Expres2 If expres1 is greater than or equal to expres2, the result is true
Expres1-lt Expres2 If expres1 is less than
Expres1-le Expres2 If expres1 is less than or equal to
! Expres if the Expres is false, the result is true

File condition test Results
-D file if it is a directory, the result is true
-e file if it exists, the result is true (usually using-f)
-F file If it is a normal file, the result is true
-G file If the Set-group-id bit is set, then
-R file if it is readable, then
-S file if the size of the files is not 0, then
-u file if the Set-user-id bit is set, then
-W file if the files are writable, then
-X file if the file is executable,

Control structure

The shell has a set of control structures that are similar to the control structures in other programming languages.
1.if Statements
If Conditon
Then
Statement
Else
Statement
Fi

2.elif Statements

3. When judging a condition, be careful to enclose the variable in quotation marks so that an empty variable provides a legitimate test.
4.for Statements
For variable in values
Do
Statement
Done
5.while Statements
Because all shell variable values are considered to be strings by default, the For loop is particularly useful for looping through a series of strings, but if you do not know in advance how many times the loop will be executed, use while.
While condition do
Statement
Done
6.until statements
Until condition
Do
Statement
Done
7.case Statements
Case variable in
pattern [| pattern] ...) statement;;
pattern [| pattern] ...) statement;;
Esac

8. List of commands
Sometimes it is necessary to concatenate multiple commands into a sequence.
and list
andList structure: execute the latter command only if all previous commands have been successfully executed
Statement1 && statement2 && statement3 && ...
Execute each command from left to right,&& is to check whether the return value of the previous command is true.
or list
ORThe list structure allows us continuous execution of a series of commands until a command succeeds, and subsequent commands are no longer executed
Statement1 | | Statement2 | | Statement3 | | ...
Executes each statement from the left, and if a command returns false, the next command on its right can be executed.
9. Statement block
If you want to use multiple statements where a single statement is allowed, you can construct a block of statements by enclosing them in curly braces {}.

Function
To define a shell function, simply write out its name, then a pair of empty parentheses, and then place the statement in the function in a pair of curly braces, as follows:
Function_name () {
Statement
}

--shell Programming for Linux Programming (chapter II)

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.