Pause bash script after read (stdin) loop

I am making a script that accepts input through a pipe (stdin), as in ( other_command | my_script). However, I need to pause the script and wait for the user to press the enter button after I read the entire stdin.

Here is an example script.

#!/bin/bash

if [[ -t 0 ]]; then
  echo "No stdin"
else
  echo "Got stdin"
  while read input; do
    echo $input
  done
fi

echo "Press enter to exit"
read

And it works as follows:

$ echo text | ./script
Got stdin
text
Press enter to exit
$

He skips over my last read.

However

$ ./script
No stdin
Press enter to exit
stops here
$

How can I make it readwork after reading stdin? And is there a solution that will work on both Linux and OSX?

+4
source share
1 answer

. stdin , - . ( stderr, , ).

read -p "Press enter" < "$(tty 0>&2)"

tty tty stdin, , , , stdin - . , stderr.

+4

Source: https://habr.com/ru/post/1568486/


All Articles