shell script programming basics (c)

Structured command (a)

if-then and case statements.
Here Insert Picture DescriptionHere Insert Picture Description
If-then-else statements
Here Insert Picture Description
when the if statement command returns a nonzero exit status code, bash shell executes the command in the else.
Problems nested if-then statement that the code is difficult to read, difficult to sort out a logic flow.
Another form can be used else part: elif. So no need to write more if-then statement a. elif make
continuation with another portion else if-then statement.
Here Insert Picture Description
elif statement provides a command line to another test, which is similar to the original if statement line. If the exit status after the elif command is 0, then bash will execute the command then the second part of the statement. Using this nesting method, code clearer, more understandable logic.
Here Insert Picture Description
elif statement returns a nonzero exit status value. This makes elif else block the block is executed.

Elif plurality of statements may continue to string together, form a large if-then-elif nested composition.
Here Insert Picture Description
Each command will be executed if 0 command returns the exit status based on. Remember, bash shell will be followed by the implementation of the if statement, only the first part of the return then exit status 0 in the statement will be executed.

You can use the test command to determine whether the content variable. This only requires a simple conditional expressions.
Here Insert Picture Description
When the test command to test conditions, returns an exit status of 0. This makes then statement statement in the block is executed.
Here Insert Picture Description
Square brackets define test conditions. No need to declare test command if-then statement. Note that after the first and before the second square brackets square brackets must add aBlankOtherwise it will error.
Here Insert Picture Description
Here Insert Picture Description
This script only a greater-than sign, there is no error, but the result is wrong. No explanation has become greater than the script output redirected
to. Therefore, it creates a file called hockey's. Due to the successful completion of redirection, test command returns an exit status of 0, if the statement and think that all commands are successfully concluded.
To solve this problem, we need to properly escaped greater-than sign.
Here Insert Picture Description
Comparison test, uppercase letters are considered to be less than lowercase letters. sory command Instead, use is sorted localized language settings for the system defined. Lowercase letters appear before capital letters.
Here Insert Picture Description
Here Insert Picture Description
Compound condition test
if-then statements allow you to use Boolean logic to combine test. There are two Boolean operators are available:
 [condition1] && [condition2 The] the AND Boolean operator, then let part of the command execution, two conditions must be met.
 [condition1] || [condition2] OR Boolean operator, any condition is TRUE, then part of the command executes.

Double parentheses
Here Insert Picture Description
expression can be any assignment or mathematical comparison expressions. In addition to standard mathematical operators use the test command,
Table 12-4 lists the other operators in double brackets command will be used.
Here Insert Picture Description
Can double parentheses command can also be used to assign the general command in a script used in the if statement.
note,Need not be double-braces is greater than the number of expressions escaped. This is another advanced feature offered command double parentheses.

Double square brackets : Provides advanced features for comparing strings.
Here Insert Picture Description
Both sides used the expression in parenthesestestCommand string comparison criterion employed. But it provides another characteristic test commands that are not available -Pattern Matching( pattern matching)。

if [[$ USER == r * ]]
bis string equals the right (r *) as a pattern, and pattern matching rules apply. Double square brackets to match the environment variable USER command to see if it begins with the letter r. If so, by comparison, shell will then execute the command section.

With the case command, you do not need to write all the elif statement to constantly check the value of the same variable. case command to the plurality of check values ​​using a single variable list format.
Here Insert Picture Description

Published 35 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_44710568/article/details/105072161