1

OK, the title may not be very clear. I was trying to do something very simple: loop through a file line by line, and add a simple read to wait each time for the user to press enter. But it reads from the pipe instead of stdin. I've tried to use some redirections but did not find the right incantation:

sort -R words.list |  while read p; do speak "$p"; read; done

It's pretty obvious why the above doesn't work. But can I do something like read <&0 or read -u /dev/stdin on the 2nd read ?

dargaud
  • 2,431
  • 2
  • 26
  • 39

2 Answers2

2

Duplicate input fd and use the duplicate.

# duplicate 10 fd as 0
exec 10<&0
sort -R word.list | while read p; do speak "$p"; read -u 10; done

You can redirect it for this command only:

{ sort -R word.list | while read p; do speak "$p"; read -u 10; done; } 10<&0
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

Use read <&1

sort -R word.list | while read p; do speak "$p"; read <&1; done

Bash File Descriptors

3.6.8 Duplicating File Descriptors

The redirection operator

[n]<&word

is used to duplicate input file descriptors. If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.

Order of redirections
In the shell, what does " 2>&1 " mean?

0stone0
  • 34,288
  • 4
  • 39
  • 64