shell script structured statement

This article records two kinds of loop statements in the shell: for and while

for loop

The for loop is the most commonly used structure in the linux shell. The for loop has three structures: 1. List for loop, 2. For loop without list, 3. C-style for loop.

1. List loop

for var in {list}
do
command
command
...
done

In the list loop, the number of loop executions is the same as the number of constants or strings in the list list. When the for loop is executed, the first value in the list list is first assigned to the loop body as the loop variable, and then the loop body is executed. Then assign the second value, and so on, until the last value in the list is assigned and executed, and the entire loop ends.
Example 1:

#!/bin/bash

###
#for i in `seq 1 5` #取值方式与下面一样
for i in {1..5}
do
echo $i
done

The list value can also choose the step size, for example:

#!/bin/bash

###
sum=0
for i in `seq 1 2 100`
#for i in {1..100..2} #与上面`seq 1 2 100`一样生成1到100,步长为2
do
let sum+=i
done
echo sum:$sum

without list loop

When the for loop without a list is executed, the input parameters are specified by the user, and the shell will automatically organize all the parameters input on the command line into a list in turn, and display the parameters input on the command line to the user each time until all parameters are displayed.

for var
do
command
command
...
done

Example

#!/bin/bash

###
echo "INPUT of var is: $#"
echo "input is: "
for argument
do
echo $argument
done
#执行
[root@localhost mnt]# ./te-for.sh a b c
INPUT of var is 3
input is:
a
b
c

C-like for loop

The C-like for loop is generally used when the number of loops is known; var1 in the expression is the statement that assigns the initial value of the loop variable, and the expression var2 is to determine whether to loop. When it is judged that the exit status of var2 is 0, execute The loop body between do and done, when the exit status is non-zero, will exit this loop and execute the statement after done; the expression var3 is used to change the statement of the loop variable.
Example:

#!/bin/bash

###
sum=0
for (( i=0;i<10;i++ ))
do
let sum+=i
done
echo sum:$sum

while loop

The while loop statement is also called a test loop statement. The number of repetitions of its loop is to use a condition to control whether to repeat the statement. Compared with the for loop statement, the while statement is easier to understand.

while 判断条件
do
command
command
...
done

The reason why while is named the pre-test loop is because it must first judge whether the conditions of the loop are established, and then repeat the operation. That is to say, the execution process of the while loop statement is: first judge the exit of the 'add judgment statement' Status, if the exit status is 0, execute the loop body, otherwise exit the execution loop and execute the instructions after done. In addition, there are two ways to end the loop while while, one is counting control, the other is mark matching control

  • count control
int=1
while (( ${int} <= 5 ))
do
echo $int
let int++
done
#执行
#!/bin/bash

###
int=1
while (( ${int} <= 5 ))
do
echo $int
let int++
done
  • 结束标记控制
    这种形式是等待用户输入一个参数,当输入的参数与while后的判断语句相等时,就退出整个while循环。如果不等于就等待用户继续输入参数与之对比
read var
while [ $var -ne var2 ]
do
read var
done

示例:猜数字大小

#!/bin/bash

###
read -p "input -a num(1-9) : " num
a=`echo $[RANDOM%10]`
while [ ${num} -ne ${a} ]
do
if [ ${num} -gt ${a} ]; then
echo "To high,try again"
read num
elif [ ${num} -lt ${a} ]; then
echo "Too smaill,try again"
read num
fi
done
echo "good lock"
#执行
[root@localhost mnt]# ./te-for.sh
input -a num(1-9) : 44
To high,try again
2
Too smaill,try again
5
Too smaill,try again
6
Too smaill,try again
7
Too smaill,try again
8
good lock

嵌套循环

一个循环体内又包含另一个完整的循环体,外部循环每次执行都会触发内部循环,当内部所有循环执行完一遍才会执行下一次外部循环
示例:

#!/bin/bash

###
for (( i = 1; i <= 9; i++ ))
do
for (( j=1; j<=i; j++ ))
do
let temp=i*j
echo -n "$i*$j=$temp "
done
echo " "
done
#执行
[root@localhost mnt]# ./te-for.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

循环控制符

有时候需要从循环体重退出循环,退出整个循环或者本次循环,如果是退出整个循环体,则可以使用break,如果是退出本次循环,则可以使用continue

示例:continue

for  (( i=1; i<=5; i++  ))
do
if [ $i -eq 3 ]; then
continue
fi
echo $i
done
#执行
[root@localhost mnt]# ./te-for.sh
1
2
4
5

示例:break

for  (( i=1; i<=5; i++  ))
do
if [ $i -eq 3 ]; then
break
fi
echo $i
done
#执行
[root@localhost mnt]# ./te-for.sh
1
2

补充内容

外部重定向循环

while还支持从外部文件读取内容

while read line
do
echo $line
done < /file/path

这种方式中while将会一行一行取文件中的内容并将读取的内容定义成变量$line,送到while循环体中进程操作

示例:

#!/bin/bash
#
while read line
do
echo $line
done < /etc/passwd
#执行
[root@localhost mnt]# ./test.sh
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...省略...

指令重定向

while还可以将指令执行的结果重定向到while循环体中
示例:判断/var目录下的文件类型

#!/bin/bash

find /var -maxdepth 1 | while read line
do
if [ -d $line ];then
echo "$line is dirctory"
elif [ -f $line ]; then
echo "$line is file "
elif [ -L $line ]; then
echo "$line is links "
else
echo "$file is other file type"
fi
done

while死循环

while true; do
循环体
done

示例:循环检查mysqld服务状态,如果down机就启动

#!/bin/bash

sleep 5
while true
do
pgrep mysqld
if [ $? -ne 0 ]; then
service mysqld start
else
echo "mysql server ok"
fi
sleep 5
done

Guess you like

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