In a programming language, a looping statement is one of the most basic syntaxes, and is no exception in the shell (here is bash).
This includes the For/while/until loop, as well as the syntax instance of the variable increment.
The loop statements in the Shell (for example, bash) generally have a for, while, until, and occasionally write the wrong syntax, which is summed up in conjunction with the example. Also provide a quick access to information for future use.
One, for Loop statement
Instance 1.1 most basic for loop: (traditional form, for Var in ...)
Copy Code code as follows:
#!/bin/bash
For X in one two three four
Todo
Echo Number $x
Done
Note: a "for" Loop always receives a type of word list after the "in" statement. In this example, four English words are specified, but the word list can also refer to files on disk, even file wildcard characters.
Instance 1.2 makes a for loop for a file in a directory
Copy Code code as follows:
#!/bin/bash
For x in/var/log/*
Todo
#echo "$x is a file living In/var/log"
echo $ (basename $x) is a file living In/var/log
Done
Note: This $x gets the absolute path file name, and you can use the "basename" executable program to remove the previous path information. If you refer only to a file in the current working directory (for example, if you enter "for x in *"), the resulting list of files will have no prefix for the path information.
Instance 1.3 makes a for loop on positional parameters
Copy Code code as follows:
#!/bin/bash
For thing in "$@"
Todo
echo You typed ${thing}.
Done
Instance 1.4 in the for loop the number of loops generated by SEQ, plus the C-language for Loop statement
Copy Code code as follows:
#!/bin/bash
echo "For:traditional form:for var in ..."
For j in $ (SEQ 1 5)
Todo
Echo $j
Done
echo "For:c language form:for (EXP1; EXP2; EXP3)) "
for ((I=1; i<=5; i++))
Todo
echo "I= $i"
Done
Note: For a fixed number of cycles, can be achieved by the SEQ command, there is no need for the independent increase of variables; the C-for-loop style is quite familiar here.
Two, while loop statements
Instance 2.1 loops output 1 to 10 digits
Copy Code code as follows:
#!/bin/bash
Myvar=1
While [$myvar-le 10]
Todo
Echo $myvar
myvar=$ (($myvar + 1))
Done
Note: As long as a specific condition is true, the "while" statement executes
Three, until circular statement
Instance 3.1 loops output 1 to 10 digits
The "Until" statement provides the opposite of a "while" statement: they are repeated as long as the specific conditions are false. The following is a "until" loop that has the same functionality as the previous "while" loop.
Copy Code code as follows:
#!/bin/bash
Myvar=1
Until [$myvar-GT 10]
Todo
Echo $myvar
myvar=$ (($myvar + 1))
Done
When writing loops in a Linux shell, it is often necessary to use the self increment of the variable, now summarize the method of the integer variable self increase.
As I know, in bash, the variable is growing, there are now five ways:
1. i= ' expr $i + 1 ';
2. Let i+=1;
3. ((i++));
4. i=$[$i +1];
5. i=$ (($i + 1))
Can be practiced, a simple example is as follows:
Copy Code code as follows:
#!/bin/bash
i=0;
While [$i-lt 4];
Todo
echo $i;
i= ' expr $i + 1 ';
# let I+=1;
# ((i++));
# i=$[$i +1];
# i=$ (($i + 1))
Done