您可以擺弄文件句柄,以便您仍然可以訪問舊的標準輸入。例如,該文件qq.sh
將讀取本身和使用read
循環打印每行,和也問你一個問題,每行後:
while read line
do
echo " Reading within the loop: [$line]"
echo -n " What do you want to say? "
read -u 3 something
echo " You input: [$something]"
done 3<&0 <qq.sh
它通過先保存標準輸入做到這一點(文件句柄0)進入文件處理3與3<&0
,然後使用read -u <filehandle>
變種從文件句柄3.一個簡單的成績單爲:
pax> ./qq.sh
Reading within the loop: [while read line]
What do you want to say? a
You input: [a]
Reading within the loop: [do]
What do you want to say? b
You input: [b]
Reading within the loop: [echo "Reading within the loop: [$line]"]
What do you want to say? c
You input: [c]
Reading within the loop: [echo -n "What do you want to say? "]
What do you want to say? d
You input: [d]
Reading within the loop: [read -u 3 something]
What do you want to say? e
You input: [e]
Reading within the loop: [echo "You input: [$something]"]
What do you want to say? f
You input: [f]
Reading within the loop: [done 3<&0 <qq.sh]
What do you want to say? g
You input: [g]
Reading within the loop: []
What do you want to say? h
You input: [h]
pax> _
您可能會發現'awk'成爲你試圖做一個更好的選擇。 – 2012-03-11 01:52:38