檔案迴圈輸入 - Linux

Table of Contents

我們可以用Loop輸出, 並在script外面把stdout 轉向到檔案上:
vi test.sh
#!/bin/bash
for i in {1..10}
do
echo "this is output"
done

chmod +x test.sh; ./test.sh >out; cat out


但我找書找來找去卻沒有方法讓script可以從一個檔案一行一行輸入並處理

假設有個檔案長這樣
#1
aa 1 2 3
bb 2 3 4
#2
dd 33 5 1
df 5 61 2
#3
fe 3 5 1
gg 64 12 12

有沒有人知道要怎麼弄一個script讓這檔案一行一行輸入處理?
#!/bin/bash
for (某i行檔案; i++) do
line=第i行檔案內容
if (line第一個字==#); then
..............
fi

done

--

All Comments

Christine avatarChristine2013-12-15
一次讀一行可以用 read
Una avatarUna2013-12-16
是能夠用read 但永遠只能read第一行
Carol avatarCarol2013-12-18
不曉得有沒有方法能夠shift行數去read進去
Liam avatarLiam2013-12-20
while read line; do echo "$line"; done < file
Anonymous avatarAnonymous2013-12-20
i=0;while read -r line[${i}];do i=$(( $i + 1 ))
Ina avatarIna2013-12-24
done < 檔案
echo ${line[3]} | grep -o '^.'
Ingrid avatarIngrid2013-12-26
for a in `cat file`