response checker in a script

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question


















  • 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










  • @steeldriver, indeed, you are absolutely right, thank you.
    – DonJ
    Mar 24 at 17:42















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?







share|improve this question


















  • 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










  • @steeldriver, indeed, you are absolutely right, thank you.
    – DonJ
    Mar 24 at 17:42













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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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 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













  • 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










  • @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











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





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 0:46









        Jeff Schaller

        31.2k846105




        31.2k846105






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?