fork download
  1. #!/bin/bash
  2.  
  3. cd "$TMPDIR"
  4. # create data file with 5 lines
  5. seq 5 > data.txt
  6. echo "** file data **"
  7. cat data.txt
  8.  
  9. echo
  10. echo "** case 1 ( read -u fd ) **"
  11. while read -u 3 line; do
  12. echo "from file: $line"
  13. read -p "press enter: " stop
  14. echo "your input: $stop"
  15. done 3<data.txt
  16.  
  17. echo
  18. echo "** case 2 ( read <&fd ) **"
  19. while read line <&3; do
  20. echo "from file: $line"
  21. read -p "press enter: " stop
  22. echo "your input: $stop"
  23. done 3<data.txt
  24.  
  25. echo
  26. echo "** case 3 ( read -u temporal fd ) **"
  27. exec {ifd}<data.txt
  28. while read -u $ifd line; do
  29. echo "from file: $line"
  30. read -p "press enter: " stop
  31. echo "your input: $stop"
  32. done
  33. exec {ifd}<&-
  34.  
Success #stdin #stdout 0.01s 5540KB
stdin
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj
kkk
lll
mmm
nnn
ooo
ppp
qqq
rrr
sss
ttt
uuu
vvv
www
xxx
yyy
zzz
stdout
** file data **
1
2
3
4
5

** case 1 ( read -u fd ) **
from file: 1
your input: aaa
from file: 2
your input: bbb
from file: 3
your input: ccc
from file: 4
your input: ddd
from file: 5
your input: eee

** case 2 ( read <&fd ) **
from file: 1
your input: fff
from file: 2
your input: ggg
from file: 3
your input: hhh
from file: 4
your input: iii
from file: 5
your input: jjj

** case 3 ( read -u temporal fd ) **
from file: 1
your input: kkk
from file: 2
your input: lll
from file: 3
your input: mmm
from file: 4
your input: nnn
from file: 5
your input: ooo