Shell Scripting Learning-Grammar Chapter

Source: Internet
Author: User
Tags case statement

I. Condition testing: 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).

Two. 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 language
The sentence is essentially composed of several shell commands, in fact, three commands, 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 from each other, and a single command on one line does not need to be written; number, and then there is a newline after that, but this command is not finished, the shell will automatically continue, the next line after then as a command processing. 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 not 0 (false), then the subcommands after elif, else, or 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

Three. The case command can be analogous to the C language Switch/case statement, ESAC represents the end of the case statement block. The case of the C language can only match an integer or character constant expression, and the shell script case matches the string and wildcard, and each matching branch can
To have a number of orders, at the end of which must be;; At the end of execution, find the first matching branch and execute the corresponding command, then straight
After jumping to ESAC, you do not need to jump out of the same language as C.

Four. For/do/done

Five. 5. While/do/done
The use of while is similar to the C language

Six. 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 of the function body must have spaces or line breaks, if the last command and the right flower are enclosed
Number} is written on the same line and must be at the end of the command;
When you define the Foo () function, you do not execute commands in the body of the function, just as you would define a variable, but give the name foo a definite
When invoking the Foo function (Note that the function call in the shell does not write parentheses) executes the command in the body of the function.
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 calls and other
The command is written at the end of the script (like the C language's main function, which is where the entire script actually starts executing the command).
A shell function that does not have a parameter list does not mean that arguments cannot be passed, in fact, a function is like a mini script that can be passed when calling a function
Any parameter, within the function is also used to extract parameters such as $, $, $, etc., the position parameter in the function is equivalent to the function
Local variables, changing these variables does not affect variables such as $, $, $, and so on outside the function. function, you can use return
Command returns if the return followed by a number represents the exit Status of the function.

For example:

650) this.width=650; "title=" 1.png "style=" Float:none; "src=" http://s4.51cto.com/wyfs02/M01/86/2F/ Wkiom1e3dscadpnhaab5vmh3cjc423.png-wh_500x0-wm_3-wmp_4-s_1606438118.png "alt=" Wkiom1e3dscadpnhaab5vmh3cjc423.png-wh_50 "/>

650) this.width=650; "title=" 2.png "style=" Float:none; "src=" http://s5.51cto.com/wyfs02/M01/86/30/ Wkiom1e3dsgrhbzvaaa0gljbhrq139.png-wh_500x0-wm_3-wmp_4-s_810464347.png "alt=" Wkiom1e3dsgrhbzvaaa0gljbhrq139.png-wh_50 "/>

650) this.width=650; "title=" 3.png "style=" Float:none; "src=" http://s5.51cto.com/wyfs02/M00/86/2F/ Wkiol1e3dshjsd-saaayqutypzc861.png-wh_500x0-wm_3-wmp_4-s_1076290694.png "alt=" Wkiol1e3dshjsd-saaayqutypzc861.png-wh_50 "/>


Shell Scripting Learning-Grammar Chapter

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.