我想使用嵌套for循环来创建文件。我有三个数组如下:在Shell脚本中循环嵌套无法正确迭代数组
a=(1000 2000 3000)
b=(10515 7515 4515)
c=(10 20 30 40)
使用这些阵列将指定哪些文件,我的for循环将通过(从已存在的文件复制数据,并将其复制到新创建的文件的目的)搜索。
for tom in "${tom[@]}"
do
for dick in "${dick[@]}"
do
for harry in "${harry[@]}"
do
FILENAME="this_is_file_${tom}_${dick}_c0.out"
NEWFILE="this_is_newfile_${tom}_${dick}_${harry}.out"
cat $FILENAME >> $NEWFILE
awk 'NR==n{$3=a}1' n="${x}" a="${harry}" $NEWFILE &> tmp && mv tmp $NEWFILE
mv $NEWFILE NewFiles
done
done
done
done
(我已经定义了x =“string”)。到目前为止,当for循环经历一次迭代时,我的代码会生成所有文件,下一个循环返回到最后一个数组值4515处开始并跳过第一个10515值。我得到了40个文件,这是正确的数字,但是当tom的数组值为2000和3000时,dick的数组仅使用数组值7515和4515生成文件。它重复4515两次,给出以下文件列表:
this_is_newfile_1000_10515_10.out
this_is_newfile_1000_10515_20.out
this_is_newfile_1000_10515_30.out
this_is_newfile_1000_10515_40.out
this_is_newfile_1000_7515_10.out
this_is_newfile_1000_7515_20.out
this_is_newfile_1000_7515_30.out
this_is_newfile_1000_7515_40.out
this_is_newfile_1000_4515_10.out
this_is_newfile_1000_4515_20.out
this_is_newfile_1000_4515_30.out
this_is_newfile_1000_4515_40.out
这里是它变得时髦,汤姆的数组值2000通知,迪克的价值仍然是4515,而不是循环回到10515.
this_is_newfile_2000_4515_10.out
this_is_newfile_2000_4515_20.out
this_is_newfile_2000_4515_30.out
this_is_newfile_2000_4515_40.out
this_is_newfile_2000_7515_10.out
this_is_newfile_2000_7515_20.out
this_is_newfile_2000_7515_30.out
this_is_newfile_2000_7515_40.out
this_is_newfile_2000_4515_10.out
this_is_newfile_2000_4515_20.out
this_is_newfile_2000_4515_30.out
this_is_newfile_2000_4515_40.out
(对不起,长长的清单,我想弄清楚发生了什么事)这重复了3000的汤姆斯数组值。任何人都可以帮助吗?关于使用awk的行,我还有另一个问题,我将在另一个条目中发布。
''b $ [@]}'中没有大括号,''c {[@]}“'中没有美元,一个'做得太多了 - 你实际上运行了吗? –
当我在这里输入时,这是一个错字。那些不是我的问题。 –