For, while,
1. For command
ForVaRInList
Do
Commands
Done
Provide a series of values for iteration In the parameter list.
(1) read values from the list
VaR is used to obtain the value in the list in this loop. In addition, the variable VAR retains the value of the last loop after the loop exists, and can continue to be used.
(2) read complex values from the list
Question 1:
When list values contain single quotes, they are used to define a separate data value.
There are two ways to solve this problem:
Escape single quotes using escape characters (backslash)
Use double quotation marks to define the values that use single quotation marks
Question 2:
Run multi-character values
That is, if there are data values that contain spaces, they must be surrounded by double quotation marks.
(3) read the list from the variable
You can use a variable to include the standard text value list for iteration, and you can also use a value assignment statement to add a project to an existing list contained in the variable.
(4) read the value in the command
You can use the reversed quotation marks to execute any command that generates the output, and then use the command output in the for command.
(5) change the field separator
The environment variable IFS defines the character list that bash shell uses as the field separator. By default, bash shell willSpace, tab, line feedAs a field separator.
Therefore, when processing data that requires spaces, it will be processed into multiple data fields. To solve this problem, you can temporarily change the IFS value of the environment variable in the shell script, restrict bash shell to the characters that are used as field delimiters.
For example, you can change the value of IFS to only recognize the line break: IFS = $ '\ n' to include spaces and tabs in each data field.
To specify multiple ifs characters, you only need to concatenate them in the value assignment line.
Ex: Use line breaks, colons, semicolons, and double quotation marks as field delimiters: IFS = $ '\ N ':;"
(6) use wildcards to read Directories
1) You can use the for command to automatically iterate the directory of the file, that is, force shell to useFile configuration, that is, the process of generating a file or path name that matches the specified wildcard.
This function is used when you do not know all file names in the directory.
2) by listing a series of directory wildcards in the for command, you can combine the Directory Search Method with the list method in the same for statement.
The for statement first uses the file list generated by the file wildcard, and then iterates the next file in the list by combining any number of wildcards in the list.
Note: It is a good habit to check whether a file or directory exists before you try to process it.
2. C-type for command (1) for command in C Language
Basic Format: ((Variable assignment; condition; iteration process))
Note that there are several for methods that do not follow the standard bash shell:
1) The variable value can contain spaces.
2) the variables in the condition are not prefixed with dollar signs.
3) iterative processing without using the expr Command Format
(2) Use multiple variables
The C-type for command can also use multiple variable iterations, but only one condition can be defined in the for loop.
3. While command
(1) Basic Format
WhileCommand
Do
Other commands
Done
The most common is to use the test command parentheses to check the shell variable values used for loop commands.
(2) Use multiple test commands
BASH Shell allows you to define multiple test commands in the while statement line.But in fact, only the exit status of the last test command is used to determine when the loop stops..
In the while statement of multiple commands, all test commands are executed in each iteration, including the last iteration of the test Command failure.
4. Until command
(1) The until command is just the opposite of the while command. A test command must be created, and a non-0 exit state is usually generated. As long as the exit status of the test command is not 0, the internal loop continues to be executed. Once the test command returns 0, the loop stops.
(2) Basic Format:
UntilCommands
Do
Other commands
Done
(3) similar to while, you can also use multiple test commands in the until command statement.Only the exit status of the last command can determine whether bash shell executes other defined commands.
5. nested loops
A loop statement can use any type of commands in a loop, including other loop commands.
Note: because another iteration is executed within an iteration, it increases the number of running commands.
Hybrid loops are also used.
6. file data circulation
When You Need To iterate the items stored inside the file, you need to use nested loops and change the environment variable IFS
Change the ifs so that the for command can process a line in the file as a separate item.
After extracting a row, you need to process the individual data in the row. You need to set up ifs again and perform an internal nested loop.
This is a good way to process data separated by delimiters in text, and is also a common method to import data to workbooks.
7. Control Loop (1) break command
You can use break to jump out of any type of loop. The following methods are available:
1) jump out of a single loop: when there is only one loop, terminate it directly.
2) jump out of the internal loop: When a nested loop exists, terminate the innermost internal loop.
3) jump out of the External Loop: it may be in an internal loop sometimes, but it needs to jump out of the External Loop. You can use the break command to include the parameter values:
Break n (n indicates the level of the loop to jump out, which is 1 by default, that is, counting outward from the current layer)
(2) Continue command
1) Stop this loop, skip the statement after continue, and start the next loop immediately.
Note: If the statement that adds the test condition variable is in the skipped code, an error occurs.
2) The continue command can also use parameters to specify the loop level to continue
Continue n
(N indicates the loop level to jump out. The default value is 1, that is, counting from the current layer)
8. processing loop output
You can use pipelines or redirect loop output results in shell scripts.
Add a processing command at the end of the done command to achieve this,
Ex1: done> output.txt (redirected output)
(The output in front of doneis no longer printed in the middle, and is fully written into output.txt ).
Ex2: Done | sort (pipe output)
(The output sequence generated before done is sorted and then output ).