- If-then-else-fi
- If-then-elif--then-else-fi
["$yn" = = "Y"] | | ["$yn" = = "Y"] (Boolean logic simplifies the possible return values to TRUE or FALSE type)
can also be written ["$yn" = = "Y"-o "$yn" = = "Y"]
Use of the IF condition test statement mixed Shell script parameter
Case Condition Test Statement
- Case-in-esac
The case command compares the specified variable to a different pattern. If the variable and pattern are matched, the shell executes the command specified for the pattern. multiple patterns can be separated by the | Vertical line Operation Noon . * asterisks capture all values that do not match the known pattern.
For Condition Loop statement
- In the first cycle, the content of the $var is con1;
- In the second cycle, the content of the $var is con2;
- In the third cycle, the content of the $var is Con3;
- ......
$animal variable retains its value, it also allows us to modify its value and use it in the same way as other variables outside of the for Command loop.
when single quotes appear in the list values for the For loop , the shell tries to use them to define a separate data value. If you want the normal output single quotation mark ', you must use an additional escape character (backslash \) to escape the single quotation mark, or use the double quotation mark "" to define the value used in single quotation marks.
The for loop assumes that each value in the list is separated by a space. If there are spaces in a separate data value, you must enclose the values in double quotation marks (the shell does not use double quotes as part of the value when using double quotes around a value).
Reading a list from a variable
Reading a value from a command
Each row in the Users.txt file has a value, so each value is not separated by a space. The for command iterates through the output of the cat command one line at a time.
If the value in a row has a space character, the for command will still divide the value by a space into multiple. The internal field delimiter (internal field separator) IFS environment variable must be changed at this time. By default, the bash shell treats spaces, tabs, line breaks as field separators, and the even if data has a any of these three characters, which the bash shell assumes as the start of a new data field. To solve this problem, you can temporarily change the value of the IFS environment variable in the shell script to limit the character (ifs=$ ' \ n ') that the bash shell treats as the field delimiter.
IFS in the shell:
IFS doubts in the shell:
How does a file directory with spaces work? :
If you follow the default IFS values, "Dayo y" is divided into "Dayo" and "Y" two data processing.
Suppose you want to traverse a colon-delimited value in a file (for example, in a/etc/passwd file), you need to set the value of the IFS to a colon
Ifs=:
If you want to specify more than one IFS character, simply string them up in the line of the assignment.
ifs=$ ' \ n ':;"
This assignment will use line breaks, colons, semicolons, and double quotation marks as field separators.
ifs=$ ' \ n ', ifs= ' \ n ' or ifs= ' \ n ' difference:
The exact meaning of ifs=$ ' \ n ':
- C language-style for loop command (numeric processing)
- Initial value: The initial value of a variable in the loop;
- Limit value: When the value of the variable is within the range of the limit value, the loop continues;
- Execution step: The amount of variation in the variable each time the loop is made.
Shell loop variable delivery problem:
Scope of variables in the shell loop:
While conditional loop statement
- While-do-done
You can define multiple test commands on a while statement line, and each test command must appear on a separate line. But only the exit status code of the last Test command is used to determine when to end the loop . In a while statement with multiple commands, all test commands are executed in each iteration, including the last iteration of the test command failure.
Until conditional loop statement
Example exercises:
- nesting loops, modifying IFS environment variables, and processing of outputs: looping /etc/passwd file data and redirecting output to the Usersoutput.txt file (also supports pipeline command processing)
- Specify the format to get test conditions from a file: Create multiple users, modify IFS according to the format of the input file Users.csv, get the variable by the Read command, add a new user with the Useradd command
Control loop break, continue command (for a, while, until loop, usage and action similar to C language, no demo)
- Break N: # Specifies the loop level to jump out. By default, n is 1, which indicates that the current loop is jumping out. Setting n as the 2,break command jumps out of the next level of external loops.
- Continue N: = Specifies the loop level to abort. By default, n is 1, indicating that the current loop is aborted. Setting n as the 2,continue command aborts the next level of external loops.
If, case, for, while, until in shell scripts