bash tty input drain (or flush?)












0















I am using bash and I am trying to drain the stdin up to a given char and start reading then. The input come from the tty given by my terminal emulator (xterm, *-terminal, etc...)



#!/bin/bash
typeset a=''
stty -echo
while :
do
echo -ne ""
while :; do read -r -n 1 a ; [[ "$a" = ":" ]] && break; done
read a ; echo "got a='$a'"
done


I then put this in a scratch buffer in my editor so I can cut it (mouse)



bad
bad
bad
bad
: good
bad
bad
bad
bad
: good


Then I run my t.sh script and doing mouse paste repeatedly I got this



TC$ ./t.sh
got a='good'
got a='bad'
got a='bad'
got a=': good'
got a='bad'
got a='bad'
got a=': good'
got a='bad'
got a='bad'


Is that expected?
Is there an way to ignore all input until a given char is found.



In a nutshell this simulate what happen on a stdin tty (ptys) when sending a query escape sequence to a terminal that reply back the answer on the stdin (as if typed on the keyboard) and the operator, at the same time, polute the input flow by for instance doing auto repeat on [enter]
The terminal do emit a header that we can scan to drain the input, but with bash it seems hard to do.



Any help welcome :)










share|improve this question



























    0















    I am using bash and I am trying to drain the stdin up to a given char and start reading then. The input come from the tty given by my terminal emulator (xterm, *-terminal, etc...)



    #!/bin/bash
    typeset a=''
    stty -echo
    while :
    do
    echo -ne ""
    while :; do read -r -n 1 a ; [[ "$a" = ":" ]] && break; done
    read a ; echo "got a='$a'"
    done


    I then put this in a scratch buffer in my editor so I can cut it (mouse)



    bad
    bad
    bad
    bad
    : good
    bad
    bad
    bad
    bad
    : good


    Then I run my t.sh script and doing mouse paste repeatedly I got this



    TC$ ./t.sh
    got a='good'
    got a='bad'
    got a='bad'
    got a=': good'
    got a='bad'
    got a='bad'
    got a=': good'
    got a='bad'
    got a='bad'


    Is that expected?
    Is there an way to ignore all input until a given char is found.



    In a nutshell this simulate what happen on a stdin tty (ptys) when sending a query escape sequence to a terminal that reply back the answer on the stdin (as if typed on the keyboard) and the operator, at the same time, polute the input flow by for instance doing auto repeat on [enter]
    The terminal do emit a header that we can scan to drain the input, but with bash it seems hard to do.



    Any help welcome :)










    share|improve this question

























      0












      0








      0


      1






      I am using bash and I am trying to drain the stdin up to a given char and start reading then. The input come from the tty given by my terminal emulator (xterm, *-terminal, etc...)



      #!/bin/bash
      typeset a=''
      stty -echo
      while :
      do
      echo -ne ""
      while :; do read -r -n 1 a ; [[ "$a" = ":" ]] && break; done
      read a ; echo "got a='$a'"
      done


      I then put this in a scratch buffer in my editor so I can cut it (mouse)



      bad
      bad
      bad
      bad
      : good
      bad
      bad
      bad
      bad
      : good


      Then I run my t.sh script and doing mouse paste repeatedly I got this



      TC$ ./t.sh
      got a='good'
      got a='bad'
      got a='bad'
      got a=': good'
      got a='bad'
      got a='bad'
      got a=': good'
      got a='bad'
      got a='bad'


      Is that expected?
      Is there an way to ignore all input until a given char is found.



      In a nutshell this simulate what happen on a stdin tty (ptys) when sending a query escape sequence to a terminal that reply back the answer on the stdin (as if typed on the keyboard) and the operator, at the same time, polute the input flow by for instance doing auto repeat on [enter]
      The terminal do emit a header that we can scan to drain the input, but with bash it seems hard to do.



      Any help welcome :)










      share|improve this question














      I am using bash and I am trying to drain the stdin up to a given char and start reading then. The input come from the tty given by my terminal emulator (xterm, *-terminal, etc...)



      #!/bin/bash
      typeset a=''
      stty -echo
      while :
      do
      echo -ne ""
      while :; do read -r -n 1 a ; [[ "$a" = ":" ]] && break; done
      read a ; echo "got a='$a'"
      done


      I then put this in a scratch buffer in my editor so I can cut it (mouse)



      bad
      bad
      bad
      bad
      : good
      bad
      bad
      bad
      bad
      : good


      Then I run my t.sh script and doing mouse paste repeatedly I got this



      TC$ ./t.sh
      got a='good'
      got a='bad'
      got a='bad'
      got a=': good'
      got a='bad'
      got a='bad'
      got a=': good'
      got a='bad'
      got a='bad'


      Is that expected?
      Is there an way to ignore all input until a given char is found.



      In a nutshell this simulate what happen on a stdin tty (ptys) when sending a query escape sequence to a terminal that reply back the answer on the stdin (as if typed on the keyboard) and the operator, at the same time, polute the input flow by for instance doing auto repeat on [enter]
      The terminal do emit a header that we can scan to drain the input, but with bash it seems hard to do.



      Any help welcome :)







      bash input flush






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 12:00









      PhiPhi

      14512




      14512
























          2 Answers
          2






          active

          oldest

          votes


















          0














          With @triplee suggestion



          #!/bin/bash

          typeset a=''
          stty -echo
          while :
          do
          read -d :
          read a ; echo "got a='$a'"
          done


          scratchpad to cut/paste into t.sh running



          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good


          Run



          TC$ ./t.sh
          got a='good'
          got a='bad'
          got a='good'
          got a='bad'
          got a='bad'
          got a=': good good'





          share|improve this answer


























          • I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

            – tripleee
            Jan 22 at 5:45



















          0














          read -d ':' _ will read input until the first colon.



          while read -d :
          do
          printf 'Preread: Got "%s"n' "$REPLY"
          read -r a
          printf 'In loop: Got "%s"n' "$a"
          done <<_
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          _


          Output:



          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"





          share|improve this answer


























          • Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

            – Phi
            Jan 20 at 13:36













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276204%2fbash-tty-input-drain-or-flush%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          With @triplee suggestion



          #!/bin/bash

          typeset a=''
          stty -echo
          while :
          do
          read -d :
          read a ; echo "got a='$a'"
          done


          scratchpad to cut/paste into t.sh running



          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good


          Run



          TC$ ./t.sh
          got a='good'
          got a='bad'
          got a='good'
          got a='bad'
          got a='bad'
          got a=': good good'





          share|improve this answer


























          • I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

            – tripleee
            Jan 22 at 5:45
















          0














          With @triplee suggestion



          #!/bin/bash

          typeset a=''
          stty -echo
          while :
          do
          read -d :
          read a ; echo "got a='$a'"
          done


          scratchpad to cut/paste into t.sh running



          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good


          Run



          TC$ ./t.sh
          got a='good'
          got a='bad'
          got a='good'
          got a='bad'
          got a='bad'
          got a=': good good'





          share|improve this answer


























          • I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

            – tripleee
            Jan 22 at 5:45














          0












          0








          0







          With @triplee suggestion



          #!/bin/bash

          typeset a=''
          stty -echo
          while :
          do
          read -d :
          read a ; echo "got a='$a'"
          done


          scratchpad to cut/paste into t.sh running



          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good


          Run



          TC$ ./t.sh
          got a='good'
          got a='bad'
          got a='good'
          got a='bad'
          got a='bad'
          got a=': good good'





          share|improve this answer















          With @triplee suggestion



          #!/bin/bash

          typeset a=''
          stty -echo
          while :
          do
          read -d :
          read a ; echo "got a='$a'"
          done


          scratchpad to cut/paste into t.sh running



          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good


          Run



          TC$ ./t.sh
          got a='good'
          got a='bad'
          got a='good'
          got a='bad'
          got a='bad'
          got a=': good good'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 13:48

























          answered Jan 20 at 13:42









          PhiPhi

          14512




          14512













          • I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

            – tripleee
            Jan 22 at 5:45



















          • I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

            – tripleee
            Jan 22 at 5:45

















          I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

          – tripleee
          Jan 22 at 5:45





          I can't repro this result. It's also not clear what it's supposed to demonstrate. Why do you have a separate read without a -d :? Demo (with a tweak to avoid the endless loop): ideone.com/QVcTck

          – tripleee
          Jan 22 at 5:45













          0














          read -d ':' _ will read input until the first colon.



          while read -d :
          do
          printf 'Preread: Got "%s"n' "$REPLY"
          read -r a
          printf 'In loop: Got "%s"n' "$a"
          done <<_
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          _


          Output:



          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"





          share|improve this answer


























          • Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

            – Phi
            Jan 20 at 13:36


















          0














          read -d ':' _ will read input until the first colon.



          while read -d :
          do
          printf 'Preread: Got "%s"n' "$REPLY"
          read -r a
          printf 'In loop: Got "%s"n' "$a"
          done <<_
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          _


          Output:



          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"





          share|improve this answer


























          • Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

            – Phi
            Jan 20 at 13:36
















          0












          0








          0







          read -d ':' _ will read input until the first colon.



          while read -d :
          do
          printf 'Preread: Got "%s"n' "$REPLY"
          read -r a
          printf 'In loop: Got "%s"n' "$a"
          done <<_
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          _


          Output:



          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"





          share|improve this answer















          read -d ':' _ will read input until the first colon.



          while read -d :
          do
          printf 'Preread: Got "%s"n' "$REPLY"
          read -r a
          printf 'In loop: Got "%s"n' "$a"
          done <<_
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          bad
          bad
          : good
          _


          Output:



          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"
          Preread: Got "bad
          bad
          "
          In loop: Got "good"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 22 at 5:48

























          answered Jan 20 at 12:04









          tripleeetripleee

          91.1k13129184




          91.1k13129184













          • Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

            – Phi
            Jan 20 at 13:36





















          • Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

            – Phi
            Jan 20 at 13:36



















          Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

          – Phi
          Jan 20 at 13:36







          Thanx for the -d, it simplify the test case, but still no joy, still got the bad input.

          – Phi
          Jan 20 at 13:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276204%2fbash-tty-input-drain-or-flush%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre