, each time the loop completes, the Var is assigned to the next entry in the list until the end of the last entry loop is completed
Syntax: Example:
For VAR in list $ cat test_for Do with X in 1 2 3 4 5 List A do Done Echo ' 2 * $X is \c ' Let X=x*2 Echo $X Done
$ test_for 2 * 1 is 2 2 * 2 is 4 2 * 3 is 6 2 * 4 is 8 2 * 5 is 10
In the example above, the keyword For,in,do and done,var represent the name of a shell variable whose assignment runs through the execution of the For loop, the list is a string of strings separated by spaces or tabs, and a string is assigned to Var for Each loop execution.
The For loop is executed as follows:
The 1.shell variable var is set equal to the first character in the list.
The commands in 2.list a are executed.
The 3.shell variable var is set equal to the next character in the list.
The commands in 4.list a are executed.
5. The loop continues to execute until the entries in each list have been cycled.
Example of a 8.for loop
Example A:
$ cat Example_a For NAME in $ (grep home/etc/passwd | cut-f1-d:) Do Mail $NAME < Mtg.minutes echo mailed Mtg.minutes to $NAME Done
Example B
$ cat Example_b For FILE in * Do
If Test-d $FILE Then Ls-f $FILE
Fi
Done
A for structure is a very flexible loop structure that allows loops to run through any list that can be generated. Using command substitution can easily generate a build list, just as the first example uses pipes and filters to produce a list. If you ask to access the same list multiple times, you may want to store it in a file. You can use the Cat command to generate a list for your for loop, as shown in the following example:
$ cat Students User1 User2 User3 User4
$ cat For_student_file_copy For NAME in $ (cat students) Do CP test.file/home/$NAME Chown $NAME/home/$NAME/test.file chmod g-w,o-w/home/$NAME/test.file Echo Done $NAME Done
$
accessing command line arguments
You can generate a list from command-line arguments:
For I in $* or for I Do do CP $i $HOME/backups CP $i $HOME/backups Done
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.