在bash中读取for循环中的两个文件夹(Read two folders in a for loop in bash)

编程入门 行业动态 更新时间:2024-10-25 09:24:00
在bash中读取for循环中的两个文件夹(Read two folders in a for loop in bash)

我实现了一个bash脚本或者我读了两个输入文件。 我希望我读取的两个文件夹同时递增各自的文件。

我的例子是:

for folder1 in $in1/*; do for folder2 in $in2/*; do file1=`basename "$folder1` file2=`basename "$folder2"` done done

我想知道是否有可能让for循环同时增加?

我提前谢谢你

I implemented a bash script or I read two input files. I want the two folders I read to increment their respective files at the same time.

My example is :

for folder1 in $in1/*; do for folder2 in $in2/*; do file1=`basename "$folder1` file2=`basename "$folder2"` done done

I wanted to know if it was possible to make a for loop to increment at the same time?

I thank you in advance

最满意答案

一种选择是将文件名存储在一对数组中:

#!/bin/bash first=( "$in1"/* ) second=( "$in2"/* ) for i in "${!first[@]}"; do first_file=${first[i]} second_file=${second[i]} done

"${!first[@]}"扩展到第一个数组中的键列表,所以i将取值0等。

如果你的数组长度不等,你将不得不决定如何处理它。 此外,每个数组中的文件将按字母顺序排列。

One option would be to store the filenames in a pair of arrays:

#!/bin/bash first=( "$in1"/* ) second=( "$in2"/* ) for i in "${!first[@]}"; do first_file=${first[i]} second_file=${second[i]} done

"${!first[@]}" expands to the list of keys in the first array, so i will take the values 0, 1, 2, etc.

If your arrays are of unequal length you will have to decide how you want to deal with that. Also, the files in each array will be in alphabetical order.

更多推荐

本文发布于:2023-08-01 03:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   两个   bash   loop   folders

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!