概述
Today we’ll learn about the while loop in shell scripts. Loops are an essential part of any programming language. When we write a code to execute a set of statements 15 times, writing the same statement again and again would be unproductive.
今天,我们将了解Shell脚本中的while循环。 循环是任何编程语言的重要组成部分。 当我们编写代码以执行一组语句15次时,一次又一次地编写同一条语句将毫无用处。
Loops save programmer’s time.
循环节省了程序员的时间。
One of the most commonly used loops in any programming language is the while loop. We use the while loop in shell scripts to repeat a set of statements as long as a given condition is satisfied after each iteration.
在任何编程语言中,最常用的循环之一是while循环。 只要在每次迭代后满足给定条件,我们就会在shell脚本中使用while循环来重复一组语句。
shell脚本中的while循环如何工作? (How does a while loop in shell scripts work?)
The while loop in shell scripts helps you make your code efficient by reducing the amount of code you need to write.
Shell脚本中的while循环通过减少需要编写的代码量来帮助您提高代码效率。
It comes handy when you have to do something in your script over and over again. Let us understand the working of a while loop in shell scripts through its syntax:
当您需要一遍又一遍地在脚本中执行某些操作时,它会派上用场。 让我们通过它的语法了解shell脚本中while循环的工作方式:
while [ condition ]
do
statement 1
statement 2
done
Here, we have three keywords, namely while, do and done.
在这里,我们有三个关键字,即while , do和done 。
- The first keyword ‘while’ indicates the beginning of the loop when we run the shell script. 第一个关键字“ while”表示运行shell脚本时循环的开始。
- It is followed by a condition enclosed in round brackets. 其次是用圆括号括起来的条件。
- The keyword ‘do’ is used before the statements to be executed in the loop. 在循环中要执行的语句之前使用关键字“ do”。
- Each while loop consists of a condition followed by a set of commands. 每个while循环均由一个条件和一组命令组成。
- Then we encounter the done keyword which marks the end of the loop. 然后,我们遇到了完成关键字,它标记了循环的结束。
Think of the do and done as opening and closing parenthesis in other programming languages
将do and done视为其他编程语言中的左括号和右括号
First, the condition is evaluated. If the condition is TRUE, the statements following do will be executed. This repeats until there is a point when the condition evaluates to FALSE.
首先,评估条件。 如果条件为TRUE,则将执行do之后的语句。 重复此操作,直到条件评估为FALSE为止。
Once the condition is false, the loop will be terminated.
一旦条件为假,循环将终止。
The important thing to keep in mind is that, like C programming, shell scripting is case sensitive. Hence, you need to be careful while using the keywords in your code.
要记住的重要一点是,与C编程一样,shell脚本也区分大小写。 因此,在代码中使用关键字时需要小心。
If you’re interested in learning about conditional statments, click here to read about if-else in shell scripts.
如果您对学习条件语句感兴趣,请单击此处以了解shell脚本中的if-else 。
在shell脚本中使用while循环 (Using while loop in shell scripts)
Just reading through the theory and glancing over the syntax would not benefit you until and unless you can practically use it in your code.
除非您可以在代码中实际使用它,否则通读理论并浏览语法不会使您受益。
Therefore, for better understanding of the concept and its practical application, check out the following example.
因此,为了更好地理解该概念及其实际应用,请查看以下示例。
While循环打印前5个数字 (While loop to print the first 5 numbers)
It is always best to start with something basic. Here we will use while loop in shell scripts to print the first 5 natural numbers on our screen.
最好总是从一些基本的东西开始。 在这里,我们将使用shell脚本中的while循环在屏幕上打印前5个自然数。
This simple script will loop while the value for the variable ‘a’ is less than (‘-lt’) the 5. The condition holds for numbers from 1 to 4, therefore, the command will be executed for the same.
当变量'a'的值小于('-lt')5时,此简单脚本将循环运行。条件适用于1到4的数字,因此,将对该命令执行相同的命令。
For any number greater than or equal to 5, the condition is false. Hence, the loop will be terminated. Here is how the code will be written.
对于大于或等于5的任何数字,条件为假。 因此,循环将终止。 这是代码的编写方式。
#!/bin/sh
a=1
while [ $a -lt 5 ]
do
echo $a
((a++))
done
在Shell脚本中使用while循环查找阶乘 (Finding the Factorial Using the while Loop in Shell Scripts)
One of the more practical examples would be using the functionality of a while loop to complete a task. Let’s find the factorial of a number.
一个更实际的示例是使用while循环的功能来完成任务。 让我们找到一个数字的阶乘。
The following code shows how we can accomplish this task using the while loop.
以下代码显示了如何使用while循环完成此任务。
#!/bin/sh
counter=5
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(( $factorial * $counter ))
((counter--))
done
echo $factorial
As you can see, the factorial for the while loop is printed correctly as 120.
如您所见,while循环的阶乘正确打印为120。
使用while循环在Shell脚本中读取文件 (Reading a File in Shell Scripts Using the while Loop)
When we are dealing with text files, while loop turns out to be an important tool as we can make the loop read the entire file using the while read command.
当我们处理文本文件时,while循环是一个重要的工具,因为我们可以使用while read命令使循环读取整个文件。
This command can help us read the contents of a file from the command line. The following code uses this functionality of the while loop in shell script to read the contents of a file one line at a time, then print it on the user’s screen.
此命令可以帮助我们从命令行读取文件的内容。 以下代码使用Shell脚本中while循环的此功能一次读取一行文件的内容,然后在用户屏幕上打印该文件。
#!/bin/bash
FILE=$1
while read line
do
# use $line variable to process line
echo $line
done < $FILE
As you can see, I’ve passed the file name as an argument because in our shell script, we have used $1 and assigned it to the FILE variable. This means the first argument will be accepted as the file name and the script will proceed to read the file.
如您所见,我已经将文件名作为参数传递,因为在我们的shell脚本中,我们使用了$ 1并将其分配给FILE变量。 这意味着第一个参数将被接受为文件名,并且脚本将继续读取文件。
Also, we use the input redirection operator within the script after the done so we can use the filename directly as an argument when executing the script.
另外,完成后我们在脚本内使用输入重定向运算符,因此在执行脚本时可以直接使用文件名作为参数。
If you skip that part, your command will look something on the lines of:
如果跳过这一部分,您的命令将看起来像以下内容:
bash file.sh < filename.txt
So you see, redirect the input within the script or while executing the script. The results are the same.
因此,您可以看到在脚本内或执行脚本时重定向输入。 结果是相同的。
结论 (Conclusion)
The while loop in shell scripts is a key weapon in every shell programmer’s arsenal. It is the best tool to use when you need to execute a set of statements repeated based on a condition.
Shell脚本中的while循环是每个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 While loop function. If you have any queries, feedback or suggestions, feel free to reach out to us in the comments below.
我们希望本教程能够帮助您了解如何使用While循环功能。 如果您有任何疑问,反馈或建议,请随时通过以下评论与我们联系。
翻译自: https://www.journaldev.com/38344/while-loop-in-shell-scripts
最后
以上就是花痴帆布鞋为你收集整理的Shell脚本中的while循环的全部内容,希望文章能够帮你解决Shell脚本中的while循环所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复