bash循环目录中的所有文件,然后循环每个文件的行

bash循环目录中的所有文件,然后循环每个文件的行

我有

urls1.txt
urls2.txt
urls3.txt 

等等。

如何循环该目录中的所有文件,然后循环这些文件的每一行?

答案1

要回答您所写的问题,您可以执行以下操作:

#!/bin/bash

for file in /path/to/files/*.txt; do
    # This will loop through each .txt file in the directory
    # setting the full file path to $file
    # I recommend adding the .txt if you know all your files
    # will have that extension otherwise the following is fine:
    # for file in /path/to/files/*; do
    while read -r line; do
        # This will loop through each line of the current file
        # and set the full line to the $line variable
        command "$line"
    done < "$file"
done

解释在代码内的注释中

相关内容