Detailed explanation of loop statements in shell scripts (for, while, until loop statements)

One, for loop statement

Read different variable values ​​to execute the same set of commands one by one.
Traversal process: execute all the content in the text once

1. Format usage

Space tab key (tab character) New line, to get the list traversed by the for loop,
execute a do operation for each traversal
Insert picture description here
Format:


for 变量名 in 取值列表        
do 
   命令序列(命令行)
done

Cycle output 1-10

for i in {
    
    1..10}          

Insert picture description here

Insert picture description here


i++    i=i+1   #自增1 
i+=2   i=i+2   #自增2 
i+=3   i=i+3   #自增3

2. For loop example

Example 1: Use the for statement to output the sum of all integers from 1 to 100

Insert picture description here
Insert picture description here

Example 2: Use the for statement to output all the even sums of 1-100

Insert picture description here
Insert picture description here

Example 3: Add all the user names in the user list to users whose password is 123123

user list:
Insert picture description here

Insert picture description here
In the user file after execution.
Insert picture description here

Example 4: Detect host status based on IP address

Host list file
Insert picture description here

Insert picture description here

Two, while loop statement

The while loop statement is more suitable for situations where it is required to control the number of cycles, the operation objects are numbered in numerical order, and the repeated operations are performed under specific conditions.

1. The structure of the while statement

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2. Take a random number, that is, a random number with a fixed number of bits

Insert picture description here

3. Guess the number game within 1000

Insert picture description here

Insert picture description here

Three, until statement

Insert picture description here

Insert picture description here

1. until statement structure

until 条件测试操作
do  
   命令序列
done

Insert picture description here

Four, commonly used escape characters

1.echo -n 表示不换行输出

2.echo -e 输出转义字符,将转义后的内容输出到屏幕上

3.常用的转义字符如下:   \b 转义后相当于按退格键(backspace),但前提是"\b"后面存在字符;"\b"表示删除前一个字符,"\b\b"表示删除前两个字符。

4.\c 不换行输出,在"\c"后面不存在字符的情况下,作用相当于 echo -n; 但是当"\c"后面仍然存在字符时,"\c"后面的字符将不会被输出。

5.\n 换行,被输出的字符从"\n"处开始另起一行。 
 
6.\f 换行,但是换行后的新行的开头位置连接着上一行的行尾;

7.\v 与\f相同;

8.\t 转以后表示插入tab,即横向制表符;

9.\r 光标移至行首,但不换行,相当于使用"\r"以后的字符覆盖"\r"之前同等长度的字符;但是当"\r"后面不存在任何字符时,"\r"前面的字符不会被覆盖

10.\\ 表示插入"\"本身;

1. Commonly used escape character -n

Insert picture description here
Insert picture description here

echo -n means not to wrap the next line
Insert picture description here

Insert picture description here

2. Commonly used -e

echo -e output escaped characters, and output the escaped content to the screen.
Use "\t" here, that is, add a TAB horizontal tab after rain.
Insert picture description here
Insert picture description here
Other escape characters are used with -e.

Five, double for loop

That is, add a for loop inside a for loop.

Using the following example here, it can be seen that when a = 1 loop is executed once, b is executed 5 times.
Insert picture description here
Insert picture description here

Six, break and continue

1.Break out of a single loop

Executing break in the inner loop jumps out of the current inner loop and executes the outer loop.
Effect:
1 jumps out of the current loop
2 and the statement below break is not executed.
We can see the characteristics of break through the output of the inner and outer loops.


#!/bin/bash
#break

for(( a=1;a<=5; a++ ))
do
  echo "outside $a"
  for ((b=1;b<=5;b++ ))
  do
     if [ $b -eq 4 ]
      then
      break                   #这里当b=4的时候,进行break,不进行后面的输出$b.
     fi
   echo "inside $b "
  done

done

Insert picture description here
When the inner loop reaches 4, it stops, and 5 is not judged.

2.continue

中止某次循环,不会完全终止整个命令
#!/bin/bash
#continue


for (( a=1;a<=15;a++))
do
    if [ $a -gt 5 ] && [ $a -lt 10 ]
    then
       continue
    fi
   echo " $a "
done

Insert picture description here

Partially terminate the loop.

Guess you like

Origin blog.csdn.net/weixin_44324367/article/details/111312156