如何在Linux Bash中循环文件?

Bash provides a lot of useful programming functionalities. for loop is one of the most useful of them. We can use for loop for iterative jobs. Linux system administrators generally use for loop to iterate over files and folder. In this tutorial, we will look at how to use for loop to iterate over files and directories in Linux. This example can be used any of Linux distribution which uses bash as shell-like Ubuntu, CentOS, RedHat, Fedora, Debian, Kali, Mint, etc. This mechanism is named as for each some programming languages where a list is iterated over.

Bash提供了许多有用的编程功能。 for loop是其中最有用的for loop之一。 我们可以将for循环用于迭代作业。 Linux系统管理员通常使用for循环来遍历文件和文件夹。 在本教程中,我们将研究如何使用for循环迭代Linux中的文件和目录。 此示例可以在使用bash的任何Linux发行版中使用,如bash一样,如Ubuntu,CentOS,RedHat,Fedora,Debian,Kali,Mint等。此机制的命名方式for each迭代列表的编程语言。

对于循环语法 (For Loop Syntax)

Bash provides different usages and syntax for the for loop but in general following syntax is used .

Bash为for loop提供了不同的用法和语法,但通常使用以下语法。

for F in ITEM1 ITEM2 ...; do
  CODE
done
  • for is the keyword which is used to create loops in bash

    for是用于在bash中创建循环的关键字

  • F is the element or item which is populated in each step from ITEMS

    F是在ITEMS的每个步骤中填充的元素或项目

  • ITEM1, ITEM2, ITEM3, … are items we want to iterate this can be also some list which contains multiple items

    ITEM1ITEM2ITEM3等都是我们要迭代的ITEM3 ,也可以是包含多个项目的一些列表

  • CODE is the implementation part which is executed in each loop

    CODE是在每个循环中执行的实现部分

数值语法(Numeric Syntax)

One of the most used Numeric syntax. We will provide numbers as a list and iterate over given list. Every number in the list will be iterated inside the for loop one by one. Every item will be assigned to the variable named $VAR like below and this variable named $VAR can be used inside the for loop book.

最常用的Numeric语法之一。 我们将提供数字作为列表,并在给定列表上进行迭代。 列表中的每个数字都将在for循环中一个接一个地迭代。 每个项目都将分配给名为$VAR的变量,如下所示,并且名为$ VAR的此变量可在for循环书中使用。

for VAR in 1 2 3 4 .. N
do
   command1 $VAR
   command2
   command3
   ...
   commandN
done

In numeric syntax, it is very similar to the general syntax where we provide a collection of numbers.

在数字语法中,它与我们提供数字集合的常规语法非常相似。

LEARN MORE  Linux fsck Command Tutorial With Examples
了解更多Linux fsck命令教程和示例

给定的文件列表语法(Given File List Syntax)

We will provide the files as a list and use them in each iteration. In this type, we will iterate over the list of files like file1, file2, file3 etc.

我们将以列表形式提供文件,并在每次迭代中使用它们。 在这种类型中,我们将遍历文件列表,如file1file2file3等。

for VAR in file1 file2 file3 file3 ... filen
do
   command1 $VAR
   command2
   command3 
   ... 
   commandN
done

命令输出语法 (Command Output Syntax)

We can use bash commands output as items for iterate. In this syntax, we expect that the $(BASH_COMMAND) will return a list where we will iterate over this list.

我们可以使用bash命令输出作为迭代项。 使用这种语法,我们期望$(BASH_COMMAND)将返回一个列表,在该列表上进行迭代。

for VAR in $(BASH_COMMAND)
do
   command1 $VAR
   command2
   command3 
   ... 
   commandN
done

循环遍历给定的文件名 (Loop Over Given File Names)

The simplest usage for for loop is over given file names. We will provide the file files by separating them with spaces. In this example, we will provide file names a , b and c and then print them with some string.

for循环最简单的用法是遍历给定的文件名。 我们将使用空格分隔文件文件。 在此示例中,我们将提供文件名abc ,然后使用一些字符串打印它们。

for f in "a" "b" "c"; 
do 
  echo Processing $f ; 
done;
Loop Over Given File Names
Loop Over Given File Names
循环遍历给定的文件名

循环列出文件名(Loop Over Listed File Names)

What can we do if there are thousands of files to be a loop in a directory. We need a more dynamic way to provide file names. We can use ls command in order to provide file names as a list without typing one by one.

如果目录中有成千上万个文件要循环,该怎么办。 我们需要一种更动态的方式来提供文件名。 我们可以使用ls命令来提供文件名作为列表,而不必一一键入。

for f in $(ls); 
do 
  echo Processing $f ; 
done;
Loop Over Listed File Names
Loop Over Listed File Names
循环列出文件名

循环遍历指定的文件扩展名(Loop Over Specified File Extensions)

Some times we may need to work on specific file extensions. We can specify the file extension we want to loop with for loop. In this example, we will print encoding types of files with *.txt extension.

有时我们可能需要处理特定的文件扩展名。 我们可以使用for循环指定要循环的文件扩展名。 在此示例中,我们将打印扩展名为*.txt的文件的编码类型。

for f in *.txt; 
do 
  file $f ; 
done;
Loop Over Specified File Extensions
Loop Over Specified File Extensions
循环指定的文件扩展名

循环读取文件中的文本文件(Loop Over Files Reading From Text File)

Files names can be stored in a text file line by line. We can read file names from the specified text file and use in a for loop. In this example, we will read the following text file and loop over lines. Our file name is filenames.txt

文件名可以逐行存储在文本文件中。 我们可以从指定的文本文件中读取文件名,并在for循环中使用。 在此示例中,我们将阅读以下文本文件并在行上循环。 我们的文件名为filenames.txt

a
b
c
d
$ for f in $(cat filenames.txt); 
do 
  echo Processing $f ; 
done;
Loop Over Files Reading From Text File
Loop Over Files Reading From Text File
循环读取文件中的文本文件

C喜欢循环(C Like For Loop)

Up to now, we have learned the basic syntax of for loop. There is also more formal for loop which is the same as C Programming language. We need to provide start condition, iterate operation and end condition.

到目前为止,我们已经了解了for循环的基本语法。 还有更正式的for循环,与C Programming语言相同。 我们需要提供开始条件,迭代操作和结束条件。

for ((START_CONDITION;ITERATE_OPERATION;END_CONDITION))
do
   COMMAND1
   COMMAND2
   ...
   COMMANDN
done

In this example, we will use echo command to print from 1 to 5 with this for loop syntax.

在此示例中,我们将使用echo命令使用此for循环语法从15打印。

#!/bin/bash
for (( c=1; c<=10; c++ ))
do 
   echo "Loop $c times"
done
C Like For Loop
C Like For Loop
C喜欢循环

无限循环(Infinite Loop)

In some cases, we may need infinite loops. The infinite loop will never end except its process is killed. We will use C like for loop in order to create an infinite loop.

在某些情况下,我们可能需要无限循环。 无限循环将永远终止,除非其进程被杀死。 我们将使用C类for循环来创建无限循环。

#!/bin/bash
for (( ; ; ))
do
   echo "This will run forever"
done
Infinite Loop
Infinite Loop
无限循环

有条件休息并退出(Conditional Break with exit)

During for loop, we may need to exit for given conditions if they occur. exit keyword can be used to break the iteration. In this example, we will check and if the current value of c can be divided into 6 we will end the loop.

在for循环中,如果发生给定条件,我们可能需要退出。 exit关键字可用于中断迭代。 在此示例中,我们将检查c的当前值是否可以分为6我们将结束循环。

#!/bin/bash
for (( c=1; c<=10; c++ ))
do
   if(($c%6 == 0)) 
   then
      exit
   fi
   echo "Loop $c times"
done

The terminal will be closed when the variable $c is equal to the 6 where the if will be true and exit statement will be executed.

当变量$ c等于6时,终端将关闭,如果if为true,则执行exit语句。

LEARN MORE  Useful Linux Commands
了解更多有用Linux命令

跳到下一步继续(Skip To Next Step with continue)

In some cases, we may need to skip the current iteration but resume to the loop. We can use continue keyword which will step over to the next iteration. In this example, we will continue if the $c variable can be divided with 3.

在某些情况下,我们可能需要跳过当前迭代,但要继续执行循环。 我们可以使用continue关键字,它将continue进行下一个迭代。 在此示例中,如果$c变量可以除以3,我们将继续。

#!/bin/bash
for (( c=1; c<=10; c++ ))
do
   if(($c%3 == 0)) 
   then
      continue
   fi
   echo "Loop $c times"
done
Skip To Next Step with continue
Skip To Next Step with continue
跳到下一步继续

翻译自: https://www.poftut.com/linux-bash-loop-files/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值