2014-10-30 50 views
0

我有以下行切如何拋出一個符合numberpairs隨機量

FileName|1.0.0.0 2.0.0.0 3.0.0.4 4.0.1.2 #.... and so on 

我檢查,如果該文件存在

if [ -f /srv/torrentfiles/$film.torrent ] 
then 

,現在我要爲每一個數字對做某事像:

while there is a new NumberPair(1.0.0.0) 
do somthing with NumberPair 

但我該怎麼做,因爲我不知道會有多少數字對。 殼有可能嗎?

回答

1

使用參數擴展來提取行的文件名,然後就遍歷的數字:

#!/bin/bash 
line='FileName|1.0.0.0 2.0.0.0 3.0.0.4 4.0.1.2' 
filename=${line%|*} 
for pair in ${line#*|} ; do 
    echo "$filename" "$pair" 
done 
+1

看起來不錯 - 也許有些雙引號將是有益的? – 2014-10-30 15:03:45

+0

@TomFenech:已添加。 – choroba 2014-10-30 15:16:46