0 script language function
A script is a production tool for system O & M personnel. It represents the productivity of maintenance. Scripts in UNIX systems have been well developed. To make some breakthroughs in the server field, windows also provides a script engine like powershell.
Scripts play an irreplaceable role in log analysis, batch modification, automatic processing, and other fields. As an explanatory language, scripts are not very efficient as an independent language, but what is more important about a script is that it combines various functions as a "adhesive" and has completed more complex work. For example, bash scripts are filled with a large number of common Linux commands and utilities, such as SED, awk, and WC.
I have been in touch with Linux for more than 10 years. Unfortunately, I have not really used Bash to do any decent work yet. I have been thinking about it for a long time. I am still learning from scratch. I am here to record my learning experience and puzzles.
1 C language coding habits are not applicable in bash
Like most IT people in China, I was also born from the IT community, C ++, C #, and Java language beginners. When learning bash programming with this background, it is often difficult to get used to it. Some good coding habits in C seem completely unfeasible in the bash world.
There cannot be spaces on both sides of the 1.1 equal sign in the C language. When writing code, we generally advocate adding spaces on both sides, such as a = 2. This good habit of Bash becomes wrong, bash requires no spaces on both sides of the equal sign. Otherwise, the syntax is incorrect! It can only be written as a = 2. Here we will analyze it a bit: Bash isolates commands and parameters through spaces. If it is written as a = 2, Bash will regard a as a command for execution, as a result, a command called A is not found and a failure is declared. 1.2 The statement does not end with a semicolon in the C language. It is the syntax requirement of the split statement, and the meaning in Bash is completely different. The statement delimiter in bash syntax is a line break symbol. A line represents a statement. If more than two statements are to appear in a line, the statements are differentiated by semicolons. For example, a = 2; B = 3 is equivalent to a = 2b = 31.3. By default, all the variables are string type analysis. What will bash output. A = 2b = 3c1 = $ A + $ BC2 = a + bc3 = $ ($ A + $ B) echo $ c1echo $ c2echo $ C3 the answer is: 2 + 3A + B5 in bash, any value is a string by default, no matter it is enclosed by quotation marks. When two strings are put together, they are connected. In this example, the initial value of the variable A is string "2", and the initial value of B is string "3". C1 refers to the variable "2", "+ ", "3" The connected strings "2 + 3"; C2 is directly initialized as the string "A + B ". The way to calculate by number is to put the arithmetic expression in $ (arithmetic expression). In this example, it is equivalent to $(2 + 3 )). Another arithmetic operation is to declare the variable type as a value rather than a string through declare-I.
1.4 The variable name and the value of the variable cannot be mixed in the C language. The variable name represents this variable. When the variable value is referenced, use the variable name directly. In bash, the value of the referenced variable must be prefixed with $ before the variable name. Let's look at the example: a = helloecho aecho $ A outputs ahello separately. Because bash treats things other than commands and Keywords as strings, when echo, it outputs A as a string. Let bash identify the variable value instead of a string by adding $. Bash searches for the value of the variable to replace $. 2 true and false2.1 true. False indicates that the command is not a constant.
The true and false values that programmers usually encounter are constants provided in the language, representing the true and false logic. In bash, this idea is no longer useful because it is a built-in bash command. It is very simple to modify the exit status code. We can test to obtain the true command and set the status code to 0, false sets the status code to 1, which is the opposite of the idea of a C-born programmer.
2.2 logic judgment is based on the exit status code
If, while, etc. c Programmers think of true and false judgments. The same is true in bash, but there is no logical variable in Bash. The logic judgment in Bash is based on the exit status code of the last command. If the previous command is successfully executed, then it is determined to be true. POSIX specifies the exit code standard: 0 indicates that the command is successfully executed; others indicate that the command is not successful. So the bash if statement is as follows:
If command
Then
Statement to be executed in real time
Else
Statement executed when it is false
When the command is successfully executed, set the status code $? = 0. This is true. Is it the opposite of what you think?
3 commands and keywords
While, for, if, then, do, done are keywords, and test or [, break, continue, return are all commands. For portability considerations, return is not recommended in vitro for functions.
4. The function has no parameters.
When a function is defined, no form parameter is allowed. The function keyword can be omitted. As follows:
Function myfun (){
Echo $1
Echo $2
Echo ($1 + $2 ))
}
When calling a function, you cannot use (). You only need to use the function name, as shown in figure
Myfun
If you want to use parameters, the function uses the location parameter. In the function body, get the parameter value through $1, $2,..., as shown above. The parameters provided are as follows:
Myfun 10 20
5. Poor Support For numeric operations
Bash cannot directly support numeric operations. The following three special methods are required:
(1) Use the expr command
A = 10
A = $ (expr $ A + 1)
(2) Use let
Let a ++
Let can only increase by 1
(3) use $ (())
A = $ ($ A + 1 ))