您可以摆弄文件句柄,以便您仍然可以访问旧的标准输入。例如,该文件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