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
dostatement 1statement 2statement 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))
dostatement 1statement 2statement 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
doecho "Executing loop $i time(s)"
done
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++ ))
doecho "Randomiser iteration $i: $RANDOM"
done
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 *
doecho "File number $((i++)) : $item"
done
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循环

shell脚本for循环_了解Shell脚本中的for循环相关推荐

  1. django中的for循环_深入了解 JavaScript 中的 for 循环

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...

  2. shell以分号结尾_一个shell脚本引发的对于分号的使用说明

    背景 由于在工作中需要一次性关闭linux的opt目录下安装的多个tomcat,因此有了写一个脚本统一关闭开启这个目录下所有tomcat的想法 前提 Tomcat安装目录结构:  /opt/tomca ...

  3. abd shell关闭所有程序_一个 Shell 脚本逆袭的规范,拿走不谢

    指定一个默认脚本解释器 "#!" 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种Shell.他指明了当我们没有指定解释器的时候默认的解释器. 为什么建议要在 ...

  4. oracle回退脚本怎么写_直播间脚本要怎么写?李佳琦、薇娅直播间直播脚本解析!...

    "口红一哥"李佳琦为何那么牛,有人说是因为站在了风口上,有人说是因为他标志性的吆喝声:事实上,更大的因素在于他对每场直播的精心准备. 比如,李佳琦推荐每款产品时,往往都会提炼出一个 ...

  5. python自动化测试脚本怎么编写_编写自动化测试脚本心得---菜鸟入门篇

    编写自动化测试脚本心得 -------- 菜鸟入门篇 本文中将不会讲解 ISEE 的测试原理.不说明 Python 的常用语法.不介绍 OTP 测试平 台的架构, 自动化测试组的牛人们已经为我们编写了 ...

  6. windows脚本编制引擎_说说 Windows 脚本宿主运行的几种方式

    1.在命令提示符下运行脚本 使用 Windows 脚本宿主,可以在命令提示符下运行脚本.CScript.exe 提供了用于设置脚本属性的命令行开关. 使用 CScript.exe 运行脚本 用下列语法 ...

  7. 的 while循环_十八、Python图解while循环

    人生苦短,要学Python Python中循环有while循环和for循环,接下来将介绍Python中的while循环和for循环. while循环 语法格式 # while语句用于循环执行程序,也就 ...

  8. python until语句_详解Lua中repeat...until循环语句的使用方法

    与for和while循环不同,在循环的顶部测试循环条件,Lua编程语言的repeat...until 循环检查循环底部的状态. repeat...until 循环类似于while循环,不同的是do . ...

  9. 跳出多重循环_代码里的俄罗斯套娃 | 07 多重循环

    点击上方 蓝字 关注我们 前情提要:刚上二年级的小红正在学习九九乘法表,老师说明天上课时要抽查,但她总是记不熟,你能写个程序帮帮她吗? 是不是想到了我们上一期学的For循环遍历,刚好可以用上.但好像又 ...

最新文章

  1. 手把手教你实现GAN半监督学习
  2. mybatis框架--学习笔记(下)
  3. Sublime Text 3103 Crack 破解 注册码(亲测有效)
  4. 3.type关键字.rs
  5. Jmeter逻辑控制器-ForEach Controller
  6. C++编程基础一 27-二维数组
  7. 将url参数转为json对象
  8. 安装微软ISA系统,安全浏览外网站点
  9. 使用xshell上传文件
  10. [转载]this 指向详细解析(箭头函数)
  11. (Android) 如何使用HOOK实现动态注入以及自动化操作
  12. Tyvj P1864 守卫者的挑战
  13. 从标准到开源,阿里大淘宝技术的“创新担当”
  14. Android怎么查看手机中的本地数据库
  15. json对象与json字符串互转,ajax各常见函数
  16. 如何安装adb(Android Debug Bridge)
  17. monkeyrunner +python 多台真机 多线程
  18. 那些年不小心来到的外国网站
  19. containerd 全面攻略
  20. [MR]曲柄滑块机械臂奇点 Crank-slider mechanism Singularities

热门文章

  1. DirectX (13) 粒子系统
  2. 计算直方图中面积最大的矩形
  3. DbEntry 访问Access2010数据库
  4. Activity与Intent机制的学习笔记--转自feisky
  5. [转载] python eval序列化函数
  6. [转载] Python字符串常用操作命令
  7. Vue.js 学习笔记 七 控制样式
  8. ethtool---查看网卡
  9. [CTSC2008] 网络管理
  10. 27_线程池_线程池实现原理