我实现了一个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 doneI 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.
更多推荐
发布评论