本文共 5690 字,大约阅读时间需要 18 分钟。
shell脚本for循环
Continuing on from our previous tutorials, let’s understand the for loop in shell scripts today. We’ve already covered the previously and the for loop has similar usage with a different format.
继续我们之前的教程,今天让我们了解shell脚本中的for循环。 我们之前已经介绍过 ,而for循环的用法与此类似,但格式不同。
Loops are widely used by programmers across the world. Adding loops to your shell script helps you to make your code efficient by reducing the amount of code you need to write.
循环被全世界的程序员广泛使用。 在shell脚本中添加循环可以通过减少需要编写的代码量来帮助您提高代码效率。
This is done by automating the execution of a set of statements in your shell script instead of writing them repeatedly.
这是通过在shell脚本中自动执行一组语句来完成的,而不是重复编写它们。
We use the for loop in shell scripts for cases when we know the number of times we wish our loop to run. This number of iterations can be specified by a list of items. Let us understand the working of the for loop in shell scripts.
当我们知道希望循环运行的次数时,可以在外壳程序脚本中使用for循环。 可以通过项目列表指定此迭代次数。 让我们了解shell脚本中for循环的工作方式。
There are two ways to run a for loop in shell scripts.
有两种方法可以在Shell脚本中运行for循环。
for var in val_1 val_2 val_3 ... val_ndo statement 1 statement 2 statement 3done
Here we have four keywords, namely for, in, do and done.
在这里,我们有四个关键字,分别是for,in,do和done 。
The other way to use the for loop in shell scripts is to incorporate a C programming approach. This is how the syntax would look.
在外壳程序脚本中使用for循环的另一种方法是合并C编程方法。 这就是语法的样子。
for (( initialization parameter; condition; updation))do statement 1 statement 2 statement 3done
Here, we replace the “in” keyword for a more C-like approach.
在这里,我们将“ in”关键字替换为更类似于C的方法。
We are now familiar with the concept and the working of the for loop in shell scripts. But to properly understand a command, you need to be able to use it in your code.
现在,我们已经熟悉了shell脚本中for循环的概念和工作原理。 但是要正确理解命令,您需要能够在代码中使用它。
Let’s go through some examples of practical usage of for loops
我们来看一些for循环的实际用法示例
We start with something simple and basic. Here, we use the first method of using the for loop.
我们从简单和基础开始。 在这里,我们使用第一种使用for循环的方法。
We use the keyword in to run a loop for 5 iterations while telling the user the number of times the loop has been executed. This is how the loop should look in your code.
在告诉用户执行循环的次数时,我们使用关键字in来运行一个循环5次迭代。 这就是循环在代码中的外观。
#!/bin/shfor i in 1 2 3 4 5do echo "Executing loop $i time(s)"done
Now we move towards an example for using the C-like variant of the For loop. We all know how we can use inbuilt commands to generate pseudo-random numbers with a shell script.
现在我们来看一个使用For循环的类似C的变体的示例。 我们都知道如何使用内置命令通过Shell脚本生成伪随机数。
Here, we will use this knowledge to generate a list of 5 pseudo-random numbers.
在这里,我们将使用此知识来生成5个伪随机数的列表。
#!/bin/shfor (( i=1; i <= 5; i++ ))do echo "Randomiser iteration $i: $RANDOM"done
Dealing with a large number of files and directories one by one takes a long time. Typing the same command over and over is a waste of time.
一个接一个地处理大量文件和目录需要很长时间。 一遍又一遍地键入相同的命令是浪费时间。
So we should take advantage of the for loop to automate the execution of a command. Let us have a look at the sample shell script below to print the name of all the files and directories present in your home directory.
因此,我们应该利用for循环来自动执行命令。 让我们看一下下面的示例shell脚本,以打印您的主目录中存在的所有文件和目录的名称。
#!/bin/shi=1cd ~for item in *do echo "File number $((i++)) : $item"done
The for loops are a powerful tool for shell programmers. It is the best tool to use when you need to execute a set of statements a fixed number of times.
for循环是Shell程序员的强大工具。 当您需要执行一组固定次数的语句时,它是最好的工具。
By automating the execution of specific statements you not only need to write less code, but you also free up time which can be used in more important tasks such as debugging.
通过自动执行特定语句,您不仅需要编写更少的代码,而且还可以节省时间,这些时间可以用于更重要的任务(如调试)中。
We hope this tutorial was able to help you understand how to use the For loop function. If you have any queries, feedback or suggestions, feel free to reach out to us in the comments below.
我们希望本教程能够帮助您了解如何使用For循环功能。 如果您有任何疑问,反馈或建议,请随时通过以下评论与我们联系。
翻译自:
shell脚本for循环
转载地址:http://mqlzd.baihongyu.com/