shell脚本for循环_了解Shell脚本中的for循环

shell脚本for循环

Continuing on from our previous tutorials, let’s understand the for loop in shell scripts today. We’ve already covered the while loop previously and the for loop has similar usage with a different format.

继续我们之前的教程,今天让我们了解shell脚本中的for循环。 我们之前已经介绍过while循环 ,而for循环的用法与此类似,但格式不同。

入门 (Getting started)

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循环的工作方式。

如何在Shell脚本中创建for循环? (How to Create a for Loop in Shell Scripts?)

There are two ways to run a for loop in shell scripts.

有两种方法可以在Shell脚本中运行for循环。

1.使用“ in”关键字 (1. Using the “in” Keyword)


for var in val_1 val_2 val_3 ... val_n
do
    statement 1
    statement 2
    statement 3
done

Here we have four keywords, namely for, in, do and done.

在这里,我们有四个关键字,分别是for,in,dodone

  1. The first keyword ‘for’ specifies the beginning of the loop when we run our shell script.

    第一个关键字“ for”指定了运行Shell脚本时循环的开始。
  2. It is followed by a variable that can take values specified by val_1, val_2 and so on.

    紧随其后的是一个变量,该变量可以采用val_1,val_2等指定的值。
  3. The keyword in specifies the beginning of this list of values. These values must be separated by ‘spaces’, as any other character such as a comma will be treated as part of the ‘value’.

    关键字in指定此值列表的开头。 这些值必须用“空格”分隔,因为其他任何字符(例如逗号)都将被视为“值”的一部分。
  4. The keyword do is used before the statements that we wish to execute

    在要执行的语句之前使用关键字do
  5. done signifies the end of our loop.

    完成表示循环已结束。

2.使用C风格的初始化 (2. Using C-style initialization)

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 3
done

Here, we replace the “in” keyword for a more C-like approach.

在这里,我们将“ in”关键字替换为更类似于C的方法。

  1. Before the loop begins the first iteration, the initialization parameter is used to initialize a variable that will act as a counter for the number of loops that our loop runs for

    在循环开始第一次迭代之前,初始化参数用于初始化变量,该变量将作为我们循环运行的循环次数的计数器
  2. As long as the condition evaluates to TRUE, the loop will execute all the statements written between “do” and “done“.

    只要条件的计算结果为TRUE,循环就会执行在“ do ”和“ done ”之间编写的所有语句。
  3. Finally, the last parameter “updation” is used to update the variable acting as the counter.

    最后,最后一个参数“ updation ”用于更新用作计数器的变量。

Shell脚本中的for循环示例 (Examples of for loop in shell scripts)

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循环的实际用法示例

1.创建一个基本的for循环 (1. Creating a basic for loop)

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/sh
for i in 1 2 3 4 5
do
  echo "Executing loop $i time(s)"
done
For Loop Basic
For Loop Basic
对于循环基本

2.在Shell脚本中使用for循环生成数字的随机列表 (2. Generating a Random List of Numbers using for Loop in Shell Scripts)

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/sh
for (( i=1; i <= 5; i++ ))
do
 echo "Randomiser iteration $i: $RANDOM"
done

For Loop Randomizer
For Loop Randomizer
对于循环随机化器

3.使用for Loop打印文件和目录的名称 (3. Printing the names of Files and Directories using for Loop)

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/sh
i=1
cd ~
for item in *
do
    echo "File number $((i++)) : $item"
done
For Loop Directories
For Loop Directories
对于循环目录

结论 (Conclusion)

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循环功能。 如果您有任何疑问,反馈或建议,请随时通过以下评论与我们联系。

翻译自: https://www.journaldev.com/38459/for-loop-in-shell-scripts

shell脚本for循环

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值