Bash can provide different programming language structures like a variable, array, loop, decision making, etc. Loop is one of the most useful features of bash scripting. Loops will give the ability to iterate over given sequential structures like an array, file list, numbers, inputs, etc.
Bash可以提供不同的编程语言结构,例如变量,数组,循环,决策等。循环是bash脚本最有用的功能之一。 循环将使您能够遍历给定的顺序结构,例如数组,文件列表,数字,输入等。
循环语法 (Loop Syntax)
Bash provides alternative syntax for a loop. Below are some examples of for loop syntax.
Bash为循环提供了其他语法。 以下是一些for循环语法的示例。
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
OR
要么
for VARIABLE in file1 file2 file3
do
command1 on $VARIABLE
command2
commandN
done
OR
要么
for OUTPUT in $(Linux-Or-Unix-Command-Here)
do
command1 on $OUTPUT
command2 on $OUTPUT
commandN
done
For line provides the loop logic and take a step for each number or file or command output. Lines between do and done is the part which will run each step.
为line提供循环逻辑,并为每个数字或文件或命令输出采取步骤。 do和done之间的界线是将运行每个步骤的部分。
循环项 (Loop Items)
One of the most popular usages of the loop is looping through items. We will provide items by separating them with space. All items will be looped. In this example, we will use numbers from 1 to 7 and define them one by one.
循环最流行的用法之一是遍历项目。 我们将以空格分隔项目。 所有项目都将循环播放。 在此示例中,我们将使用1到7之间的数字并一一定义。
$ for count in 1 2 3 4 5 6 7 ; do echo $count; done
![Loop Numbers](https://i-blog.csdnimg.cn/blog_migrate/f31501b771198677ae48de37d18e51ca.png)
循环范围(Loop Range)
We can loop numbers like above but the problem is how can we achieve if need to loop over 1000 numbers. Should we write the one by one? No, we can specify number ranges like below.
我们可以像上面那样循环编号,但是问题是如果需要循环1000个以上的数字,我们该如何实现。 我们应该一本一本地写吗? 不,我们可以指定如下所示的数字范围。
$ for count in {1..7} ; do echo $count; done
![](https://i-blog.csdnimg.cn/blog_migrate/66db545142935009fc69515fa95c548f.png)
We use {1..7}
to loop from 1 to 7 one by one.
我们使用{1..7}
从1到7逐一循环。
增值 (Increment Value)
We can also specify the increment number. Incrementing one by one is the default behavior. How can we increment 2?
我们还可以指定增量数。 默认行为是一一递增。 我们如何增加2?
$ for count in {1..5..2} ; do echo $count; done
![Increment Value](https://i-blog.csdnimg.cn/blog_migrate/57fbea79b5a9fdb8ea19ee510a41e582.png)
We have incremented loop 2 and only used numbers 1,3,5. There is an alternative way to increment for loop with 43 for statement
我们增加了循环2,仅使用了数字1,3,5。 还有一种替代方法,可使用43 for语句递增for循环
C风格的循环 (C Style For Loop)
We can specify a statement like the C programming language. We will set stat numbers, end condition and increment value like below. Here is the syntax
我们可以指定类似C编程语言的语句。 我们将设置统计数字,结束条件和增量值,如下所示。 这是语法
for (( EXP1; EXP2; EXP3 ))
do
command1
...
done
Now we will start at 1 and increment one by one to the 5.
现在,我们将从1开始,然后逐一递增到5。
$ for (( count=1;count<6;count++)) ; do echo $count; done
![C Style For Loop](https://i-blog.csdnimg.cn/blog_migrate/e6a2d9b8830d0d0c1bf9ddd756afb413.png)
无限循环(Infinite For Loop)
We can create a loop for infinity. This loop will run forever if we do not end the process.
我们可以创建一个无限循环。 如果我们不结束该过程,则该循环将永远运行。
$ for (( ; ; )) ; do echo "Kill me if you can ;)"; done
We do not provide any start number, end condition or increment value to trigger an infinite loop.
我们不提供任何开始编号,结束条件或增量值来触发无限循环。
无限While循环 (Infinite While Loop)
We can create infinite loop with a while statement. Creating an infinite loop while statement is more easy.
我们可以使用while语句创建无限循环。 创建一个无限循环的while语句更容易。
$ while true ; do echo "Kill me if you can ;)"; done
Just provide a true statement to the while.
只需提供真实的陈述即可。
循环浏览文件 (Loop Through Files)
One of the most used types of loop is looping through files in the specified directory. All filenames in the specified directory will be looped step by step.
循环最常用的一种类型是循环遍历指定目录中的文件。 指定目录中的所有文件名将逐步循环。
$ for file in $(ls -1) ; do echo $file; done
![Loop Through Files](https://i-blog.csdnimg.cn/blog_migrate/5dfcf3d295384bbcc03339014ba3f666.png)
Here the $(ls -l)
provides a list of filenames to loop and each file names is assigned to the file
variable for each step.
在这里$(ls -l)
提供了要循环的文件名列表,并且每个文件名都分配给了file
每个步骤的变量。
翻译自: https://www.poftut.com/bash-for-and-while-loop-examples/