![如何在一个循环中使用两个变量](https://linux22.com/image/81380/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%80%E4%B8%AA%E5%BE%AA%E7%8E%AF%E4%B8%AD%E4%BD%BF%E7%94%A8%E4%B8%A4%E4%B8%AA%E5%8F%98%E9%87%8F.png)
我有两个循环,但我不知道是否可以将它们放在一起:
首先,我合并同一样本中的两个不同 id:
samples="sample01 sample02 sample03"
for s in ${samples}
do
cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
done
通过这个,我计算了我的sample.txt中有多少数据是唯一的,范围在19-41之间,并将结果放入count.txt中
for ((t=17, i=1; t<43; t=t+2, i++))
do
echo "[$i] Iteration - t = $t"`
unique.py -o count.txt -t $t sample.txt
done
$t
我想要的是为每个 ${s}
sample_id12.txt 元素放置不同的值。像这样的东西:
samples="sample01 sample02 sample03"
for s in ${samples} ¿counter?
do
cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
unique.py -o count.txt -t $t ${s}_id12.txt
rm ${s}_id12.txt
done
答案1
嵌套循环示例
samples="sample01 sample02 sample03"
for s in ${samples}
do
cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
for ((t=17, i=1; t<43; t=t+2, i++))
do
echo "[$i] Iteration - t = $t"
unique.py -o count.txt -t $t ${s}_id12.txt
done
done
答案2
for ((t=17; t<43; t=t+2))
do
samples="sample01 sample02 sample03"
for s in ${samples}
do
cat ${s}_id1.txt ${s}_id2.txt > ${s}_id12.txt
unique.py -o count.txt -t $t ${s}_id12.txt
done
done
这样我就得到了文件t
中 17-41 之间的每个文件count.txt
(用每个样本文件中每个值对应的值unique.py
覆盖结果t
。
sample01_id12.txt
sample02_id12.txt
sample03_id12.txt