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
迭代列表的编程语言。
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
ITEM1
, ITEM2
, ITEM3
等都是我们要迭代的ITEM3
,也可以是包含多个项目的一些列表
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.
在数字语法中,它与我们提供数字集合的常规语法非常相似。
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.
我们将以列表形式提供文件,并在每次迭代中使用它们。 在这种类型中,我们将遍历文件列表,如file1
, file2
, file3
等。
for VAR in file1 file2 file3 file3 ... filen
do
command1 $VAR
command2
command3
...
commandN
done
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
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
循环最简单的用法是遍历给定的文件名。 我们将使用空格分隔文件文件。 在此示例中,我们将提供文件名a
, b
和c
,然后使用一些字符串打印它们。
for f in "a" "b" "c";
do
echo Processing $f ;
done;
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;
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;
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;
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循环语法从1
到5
打印。
#!/bin/bash
for (( c=1; c<=10; c++ ))
do
echo "Loop $c times"
done
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
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
语句。
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
翻译自: https://www.poftut.com/linux-bash-loop-files/