Loop Statement in Shell Script

Source: http://www.jb51.net/LINUXjishu/32574.html

           https://www.cnblogs.com/jxhd1/p/6274854.html

This includes for/while/until loops, and syntax examples for variable auto-increment.

1. For loop statement

Example 1.1 The most basic for loop: (traditional form, for var in …)

1 #!/bin/bash
2 for x in one two three four
3 do
4         echo number $x
5 done

operation result:

1 [root@my-server games]# sudo sh looptest.sh
2 number one
3 number two
4 number three
5 number four

Note: "for" loops always receive a list of words of some type following the "in" statement. In this example, four English words are specified, but word lists can also refer to files on disk, and even file wildcards.
Example 1.2 Do a for loop on the files in the directory

1 #!/bin/bash
2 for x in /var/log/*
3 do
4    echo $(basename $x) is a file living in /var/log
5 done

Results of the:

1 [root@my-server games]# sudo sh fortest2.sh
2 boot.log is a file living in /var/log
3 boot.log-20180307 is a file living in /var/log
4 boot.log-20180308 is a file living in /var/log
5 ......

Note: this $x gets the absolute path filename; the "basename" executable can be used to remove the preceding path information. If only files in the current working directory are referenced (for example, if you type "for x in *"), the resulting list of files will not be prefixed with path information.

 

Example 1.3 Use seq to generate the number of loops in the for loop, plus the for loop statement in C language

 1 #!/bin/bash
 2 echo "for: Traditional form: for var in ..."
 3 for j in $(seq 1 5)
 4 do
 5         echo $j
 6 done
 7 
 8 echo "for: C language form: for (( exp1; exp2; exp3 ))"
 9 
10 for (( i=1; i<=5; i++ ))
11 do
12         echo "i=$i"
13 done

operation result:

 1 [root@my-server games]# sudo sh fortest4.sh
 2 for: Traditional form: for var in ...
 3 1
 4 2
 5 3
 6 4
 7 5
 8 for: C language form: for (( exp1; exp2; exp3 ))
 9 i=1
10 i=2
11 i=3
12 i=4
13 i=5

Note: For a fixed number of loops, it can be implemented through the seq command, and there is no need to increment the variable; the C language for loop style here is quite familiar.

 

Two, while loop statement

Example 2.1 Looping out numbers from 1 to 10

1 #!/bin/bash
2 myvar=1
3 while [ $myvar -le 10 ]
4 do
5         echo $myvar
6         myvar=$(( $myvar + 1 ))
7 done

Results of the:

[root@my-server games]# sudo sh whiletest.sh
1
2
3
4
5
6
7
8
9
10

P.S. [-eq] [-ne] [-gt] [-lt] [-ge] [-le] in shell

-eq //equal to

-ne // not equal to

-gt // greater than (greater)

-lt // less than (less)

-ge //greater than or equal to

-le // less than or equal to

 

3. until loop statement

 Example 3.1 Looping out numbers from 1 to 10
"Until" statements provide the opposite of "while" statements: they repeat as long as certain conditions are false. Below is an "until" loop that does the same thing as the previous "while" loop.

1 #!/bin/bash
2 myvar=1
3 until [ $myvar -gt 10 ]
4 do
5         echo $myvar
6        myvar=$(( $myvar + 1 ))
7 done

The execution result is the same as the while loop result

 

When writing loops in the Linux Shell, the auto-increment of variables is often used. Now, let's summarize the method of auto-increment of integer variables.
As far as I know, in bash, there are currently five methods for variable auto-increment:
1. i=`expr $i + 1`;
2. let i+=1;
3. ((i++));
4. i=$ [$i+1];
5. i=$(( $i + 1 ))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325157137&siteId=291194637