我有一个脚本
index=0
while read line
do
echo "read line is $line"
line=`echo ${line}|awk '{print $1}'`
index=$((index+1))
if((index%2==0));then
echo "before continue line is $line"
continue
fi
echo "index is $index and now modify html page with $line"
...
...
done< infile
infile的内容是:
1OrMcP2CdV4 180
1Wp33RG2XaA 180
21zUUJ04ovI 180
2pIqUhaDMLg 180
2WRU4NUJSVc 180
...
...
我运行脚本,输出为:
read line is 1OrMcP2CdV4 180
index is 1 and now modify html page with 1OrMcP2CdV4
read line is 1Wp33RG2XaA 180
before continue line is 1Wp33RG2XaA
read line is
index is 3 and now modify html page with
read line is 21zUUJ04ovI 180
before continue line is 21zUUJ04ovI
read line is
index is 5 and now modify html page with
所以基本上,似乎“while read loop”按顺序读取行:1 3 2 5 4
然后我修改了空行,比如
1OrMcP2CdV4 180
Axxx
1Wp33RG2XaA 180
Axxx
21zUUJ04ovI 180
Axxx
2pIqUhaDMLg 180
Axxx
2WRU4NUJSVc 180
...
..
输出很有意思:
read line is 1OrMcP2CdV4 180
index is 1 and now modify html page with 1OrMcP2CdV4
read line is xxx
before continue line is xxx
read line is 1Wp33RG2XaA 180
index is 3 and now modify html page with 1Wp33RG2XaA
read line is xxx
before continue line is xxx
read line is 21zUUJ04ovI
index is 5 and now modify html page with 21zUUJ04ovI
read line is 21zUUJ04ovI 180
before continue line is 21zUUJ04ovI
好的,它现在按顺序读取,但偶数行“Axxx”读作“xxx” 对于前20行,它似乎是有序的 但随后,订单再次陷入混乱! 有趣而奇怪,不是吗?
这种行为不端有什么问题? 对于脚本,有几个后台命令,如
firefox &
tcpdump &
也喜欢
sleep 5
有关详细信息,可以参考上面的链接,那么可能存在子进程同步问题?但我不这么认为。
这个问题真的很奇怪,我真的对如何应对它感到茫然!
答案 0 :(得分:0)
所以你想获得每一行的第一个字段?然后我建议像:
while read id rest
do
# here code to process id
# .....
read again # skip every other line
done
顺便说一下:代码中index
的初始值是多少?