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?
source
share