Shell Simple Summary--grammar

Source: Internet
Author: User
Tags case statement function definition

First, condition test

The command test or [can test whether a condition is true, if the test result is real, the exit status of the command is 0, if the test result is false, the exit status of the command is 1 (note the exact opposite of the logical representation of the C language). For example, test the size relationship of two numbers:

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A7/56/wKioL1nlcEXyF9lTAACc3dg7xI4007.png "title=" Shelltest.png "alt=" Wkiol1nlcexyf9ltaacc3dg7xi4007.png "/>

Although it may seem strange, the left parenthesis [is really a command name, the arguments to the command should be separated by a space, for example, $VAR,-GT, 3,] are [four parameters of the command, they must be separated by a space. The command test or [Parameter form is the same, except that the test command does not require] parameters. For the [command] example, the common test commands are shown in the following table:

Test command:
[-D dir]: true if Dir exists and is a directory
[-F file]: True if file exists and is a normal file
[-Z string]: True if the length of the string is zero
[-N string]: True if the length of a STRING is nonzero
[STRING1 = STRING2]: True if two strings are the same
[STRING1! = STRING2]: True if the string is not the same

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A7/56/wKioL1nlcTrB-9GAAADW05sO3KE137.png "title=" Dfzshell.png "alt=" Wkiol1nlctrb-9gaaadw05so3ke137.png "/>

[ARG1 OP ARG2]: ARG1 and ARG2 should be integers or variables with integer values, OP is-eq (equals)-NE (not equals)-lt (less than)-le (less than equals)-GT (greater than)-ge (greater than or equal) one (integer judgments and C languages differ greatly). Similar to the C language, test conditions can also be done with, or, non-logical operations:
Test commands with and, or, not
[ ! Expr]: Expr can be any of the test conditions in the table above. Represents a logical inverse
[Expr1-a EXPR2]: EXPR1 and EXPR2 can be any of the test conditions in the table above,-a represents the logic and
[Expr1-o EXPR2]: EXPR1 and EXPR2 can be any of the test conditions in the table above,-o represents a logical OR

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A7/56/wKioL1nlcn3jbZEkAAB19ZZ7OMQ583.png "title=" Oashell.png "alt=" Wkiol1nlcn3jbzekaab19zz7omq583.png "/>

Note that if the $STR variable in the previous example is not defined beforehand, the shell is expanded to an empty string, resulting in a syntax error for the test condition (expanded to [-D desktop-a = ' abc '), and as a good shell programming habit, the variable value should always be placed in double quotes (expanded to [-D desktop-a "" = ' abc ']).

Second, IF/THEN/ELIF/ELSE/FI

Similar to the C language, in the shell with the IF, then, elif, else, fi these commands to achieve branch control. This process Control statement is essentially composed of a number of shell commands, such as the three command previously said, if [-F ~/.BASHRC] is the first, then. ~/.BASHRC is the second, FI is the third article. If the two commands are written in the same line, they need to be separated, and a single command on one line does not need to be written; no, and then there is a newline after that, but this command is not finished, the shell will automatically continue, and the next line is followed by then as a command. As with the command, be aware that the command and parameters must be separated by a space. If the parameter of the IF command consists of a sub-command, if the subcommand's exit status is 0 (true), then the subcommands that follow then are executed, and if exit status is 0 (for false), then the elif, else, or the subcommands behind the fi are executed. A subcommand after an if is usually a test command, but it can also be another command. The shell script does not have {} brackets, so use fi to indicate the end of the IF statement block.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/08/A7/wKiom1nleEviV5iQAAEGsBjMia8058.png "title=" Ifshell.png "alt=" Wkiom1nleeviv5iqaaegsbjmia8058.png "/>

: is a special command called the Empty command, which does nothing, but exit status is always true. In addition, you can also perform/bin/true or/bin/false to get a true or false exit Status.

The purpose of the read command in the previous example is to wait for the user to lose a line of string and to store the string in a shell variable. In addition, the shell also provides && | | syntax, similar to the C language, has short-circuit features, and many shell scripts like to write

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/08/A7/wKiom1nleQSQ9nnTAAB5u5SDvSQ949.png "title=" shell &&.png "alt=" Wkiom1nleqsq9nntaab5u5sdvsq949.png "/>

&& equivalent to "If...then ..." while | | Equivalent to "if Not...then ...". && | | Used to connect two commands, and the-A and-O mentioned above are only used to connect two test conditions in a test expression.

Third, Case\esac

The case command can resemble the C-language switch/case statement, and ESAC represents the end of the case statement block. The case of a C language can only match an integer or character constant expression, and the shell script's case matches the string and the wildcard (wildcard character), each matching branch can have several commands, which must be at the end; At the end, when execution finds the first matching branch and executes the corresponding command, and then jumps directly to ESAC, you do not need to jump out of the same language as C.

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A7/57/wKioL1nld_2QMAwfAADclpfVCa8429.png "title=" Caseshell.png "alt=" Wkiol1nld_2qmawfaadclpfvca8429.png "/>

Iv. Circular Statements

1, For/do/done

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A7/57/wKioL1nleFrhuHsCAABpSage_dA583.png "title=" Forshell.png "alt=" Wkiol1nlefrhuhscaabpsage_da583.png "/>

2, While/do/done

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A7/57/wKioL1nleJzjZVZ5AACdlwEVAcs710.png "title=" Whileshell.png "alt=" Wkiol1nlejzjzvz5aacdlwevacs710.png "/>

Five, positional parameters and special variables

  has a lot of special variables that are automatically assigned by the shell, we have encountered $? and $ 1, now summarize, the commonly used positional parameters
and special variables
$0
$1, $2...
$#
[EMAIL PROTECTED]
$?

$$
The current shell's process number
The position parameter can be shifted left with the shift command. For example, shift 3 means that the original $4 now becomes $ $, and the original $ $ now becomes a $
And so on, the original $, $, $ discarded,no move . The shift command with no parameters is equivalent to shift 1. For example:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A7/57/wKioL1nlecGQpj5pAAEYALX4tEI665.png "title=" Fuhaoshell.png "alt=" Wkiol1nlecgqpj5paaeyalx4tei665.png "/>

' basename $ ' value displays the name of the current script or command the $ show will include the path of the current script or command ' basename ' and ' $ ' are the same.

Vi. functions

Similar to the C language, there is also the concept of a function in the shell, but there is no return value in the function definition and no argument list. For example: Note that the left curly brace {and subsequent commands must have spaces or line breaks between the function body, and if the last command and the closing curly brace} are written on the same line, the end of the command must have;
When you define the Foo () function, you do not execute the command in the function body, just as you would define a variable, only a definition of the name foo, and then execute the command in the function body when you call the Foo function (note that the function calls in the shell do not write parentheses). A function in a shell script must be defined and called before the function definition is written in front of the script, and the function call and other commands are written at the end of the script (similar to the C language's main function, which is where the entire script actually starts executing the command).
The shell function does not have a parameter list does not mean that can not pass parameters, in fact, the function is like a mini script, call the function can be passed arbitrary parameters, in the function is also used by the variables such as $, $, $ to extract the parameters, the function of positional parameters equivalent to the local variables of the function, Changing these variables does not affect variables such as $, $, $, and so on outside the function. The function can be returned with the return command, if the return followed by a number represents the exit Status of the function.

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A7/59/wKioL1nljIzwfEiaAAC8MDvjS4k670.png "title=" Fun.png "alt=" Wkiol1nljizwfeiaaac8mdvjs4k670.png "/>

">/dev/null 2>&1" is commonly used to avoid content output in shell commands or programs such as running.

Vii. Debugging of Scripts

The shell provides some options for debugging scripts, as follows:
-N
Read the command in the script but not execute it to check for syntax errors in the script
-V
-X
Provides trace execution information and prints each command and result that executes sequentially
There are three ways to use these options,
One is to supply parameters at the command line
$ sh-x./script.sh

The second is to provide parameters at the beginning of the script
#!/bin/sh-x

The third method is to enable or disable parameters with the SET command in the script
The Set-x and set +x respectively indicate that the-x parameter is enabled and disabled, so that only one segment of the script can be tracked for debugging.

Shell Simple Summary--grammar

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.