【发布时间】:2012-12-01 10:31:18
【问题描述】:
我有这个文件
Seq1
10 1 5
10 2 6
10 3 9
Seq2
15 2 7
15 4 9
15 8 12
我想为每个 Seq (Seq1, Seq2) 设置这样的数组:
2ndColumn=(1,2,3)
3rdColumn=(5,6,9)
我写了这个,但它不会破坏 while 循环..
#!/bin/bash
2ndColumn=()
3rdColumn=()
while read line
do
if [[ $line == S* ]]
echo "$line"
else
i=0
while [[ $line != S* ]]
do
2ndColumn[i]="$(echo $line | cut -d\ -f2)"
3rdColumn[i]="$(echo $line | cut -d\ -f3)"
i=$((i+1))
read line
done
echo "${2ndColumn[@]} and ${3rdColumn[@]}"
fi
done < file
exit 0
此脚本将永远迭代,它不会退出 while 循环。请帮帮这个愚蠢的人:(
【问题讨论】:
-
你永远循环的原因是你的内部循环从不检查
read是成功还是失败。 -
@Barmar 为什么 while 还不够?
-
读取失败时,
line设置为空字符串,[[ $line != S* ]]为真,所以while循环一直循环。外循环使用read的成功作为其while 条件。
标签: linux arrays bash fileparsing