response checker in a script

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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 "33[31m%s33[0mn" " 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?
bash shell-script tee
add a comment |Â
up vote
0
down vote
favorite
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 "33[31m%s33[0mn" " 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?
bash shell-script tee
 
 
 4
 
 
 
 
 It's because- readis writing the prompt to- stderrbut you are only redirecting- stdoutto- teeI think; that results in the original- stderrgetting intermixed with the- teed- stdout. Try- test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
 â steeldriver
 Mar 24 at 13:29
 
 
 
 
 
 
 
 
 
 @steeldriver, indeed, you are absolutely right, thank you.
 â DonJ
 Mar 24 at 17:42
 
 
 
 
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 "33[31m%s33[0mn" " 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?
bash shell-script tee
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 "33[31m%s33[0mn" " 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?
bash shell-script tee
edited Mar 24 at 13:19


Jeff Schaller
31.2k846105
31.2k846105
asked Mar 24 at 12:55
DonJ
768
768
 
 
 4
 
 
 
 
 It's because- readis writing the prompt to- stderrbut you are only redirecting- stdoutto- teeI think; that results in the original- stderrgetting intermixed with the- teed- stdout. Try- test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
 â steeldriver
 Mar 24 at 13:29
 
 
 
 
 
 
 
 
 
 @steeldriver, indeed, you are absolutely right, thank you.
 â DonJ
 Mar 24 at 17:42
 
 
 
 
add a comment |Â
 
 
 4
 
 
 
 
 It's because- readis writing the prompt to- stderrbut you are only redirecting- stdoutto- teeI think; that results in the original- stderrgetting intermixed with the- teed- stdout. Try- test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
 â steeldriver
 Mar 24 at 13:29
 
 
 
 
 
 
 
 
 
 @steeldriver, indeed, you are absolutely right, thank you.
 â DonJ
 Mar 24 at 17:42
 
 
 
 
4
4
It's because
read is writing the prompt to stderr but you are only redirecting stdout to tee I think; that results in the original stderr getting intermixed with the teed stdout. Try test_fn /tmp/warning 2>&1 | tee -a /tmp/logfileâ steeldriver
Mar 24 at 13:29
It's because
read is writing the prompt to stderr but you are only redirecting stdout to tee I think; that results in the original stderr getting intermixed with the teed stdout. Try test_fn /tmp/warning 2>&1 | tee -a /tmp/logfileâ steeldriver
Mar 24 at 13:29
@steeldriver, indeed, you are absolutely right, thank you.
â DonJ
Mar 24 at 17:42
@steeldriver, indeed, you are absolutely right, thank you.
â DonJ
Mar 24 at 17:42
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
1
down vote
accepted
To convert a comment to an answer:
read -p writes the prompt to stderr; in order to get the results in-line for tee, pipe the function's stderr to stdout before the tee with:
test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
To demonstrate read's behavior:
$ read -p "my prompt: " >/dev/null
my prompt: hi
$ read -p "my prompt: " 2>/dev/null
hi
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
To convert a comment to an answer:
read -p writes the prompt to stderr; in order to get the results in-line for tee, pipe the function's stderr to stdout before the tee with:
test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
To demonstrate read's behavior:
$ read -p "my prompt: " >/dev/null
my prompt: hi
$ read -p "my prompt: " 2>/dev/null
hi
add a comment |Â
up vote
1
down vote
accepted
To convert a comment to an answer:
read -p writes the prompt to stderr; in order to get the results in-line for tee, pipe the function's stderr to stdout before the tee with:
test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
To demonstrate read's behavior:
$ read -p "my prompt: " >/dev/null
my prompt: hi
$ read -p "my prompt: " 2>/dev/null
hi
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
To convert a comment to an answer:
read -p writes the prompt to stderr; in order to get the results in-line for tee, pipe the function's stderr to stdout before the tee with:
test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
To demonstrate read's behavior:
$ read -p "my prompt: " >/dev/null
my prompt: hi
$ read -p "my prompt: " 2>/dev/null
hi
To convert a comment to an answer:
read -p writes the prompt to stderr; in order to get the results in-line for tee, pipe the function's stderr to stdout before the tee with:
test_fn /tmp/warning 2>&1 | tee -a /tmp/logfile
To demonstrate read's behavior:
$ read -p "my prompt: " >/dev/null
my prompt: hi
$ read -p "my prompt: " 2>/dev/null
hi
answered Mar 28 at 0:46


Jeff Schaller
31.2k846105
31.2k846105
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433261%2fresponse-checker-in-a-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
It's because
readis writing the prompt tostderrbut you are only redirectingstdouttoteeI think; that results in the originalstderrgetting intermixed with theteedstdout. Trytest_fn /tmp/warning 2>&1 | tee -a /tmp/logfileâ steeldriver
Mar 24 at 13:29
@steeldriver, indeed, you are absolutely right, thank you.
â DonJ
Mar 24 at 17:42