I have following response checker in a script:
#!/bin/bash
test_fn()
{
WARNFILE=$1
echo
echo "--- BEGIN ---"
cat ${WARNFILE}
echo "--- END ---"
echo
while true; do
read -r -n 1 -p "Continue? [y/n]: " REPLY
case $REPLY in
[yY]) break ;;
[nNqQ]) echo;exit ;;
*) printf "\033[31m%s\033[0m\n" " invalid input: ${REPLY}"
esac
done
}
test_fn /tmp/warning
it works fine...
$ ./test.sh
--- BEGIN ---
test warning
--- END ---
Continue? [y/n]: a invalid input: a
Continue? [y/n]: s invalid input: s
Continue? [y/n]: d invalid input: d
Continue? [y/n]: w invalid input: w
Continue? [y/n]: s invalid input: s
Continue? [y/n]: q
$
...until I change line:
test_fn /tmp/warning
with line:
test_fn /tmp/warning | tee -a /tmp/logfile
then, it scrambles lines:
$ ./test.sh
--- BEGIN ---
test warning
Continue? [y/n]: --- END ---
aContinue? [y/n]: invalid input: a
sContinue? [y/n]: invalid input: s
dContinue? [y/n]: invalid input: d
fContinue? [y/n]: invalid input: f
q
$
Anyone could please tell why it works so?
read
is writing the prompt tostderr
but you are only redirectingstdout
totee
I think; that results in the originalstderr
getting intermixed with thetee
dstdout
. Trytest_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
– steeldriver Mar 24 '18 at 13:29