LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > Michael Uplawski
User Name
Password

Notices


Rate this Entry

Unbuffered shell-input

Posted 10-16-2021 at 02:46 AM by Michael Uplawski
Updated 11-13-2021 at 01:43 AM by Michael Uplawski (Wrong title, much misunderstanding and ill-chosen search criteria.)

There are solutions on the Web.

Having read some of these, I prefer a Ruby-Oneliner readNB which returns the 1 character that the user has had a chance to enter on the keyboard, before readNB returns.

---------------------- In the meantime -------------------------
GazL has in the very first comment on this blog, below presented a better solution with the shell built-in command read: read accepts some parameters and a call like
Code:
read -r -s -N1 [VARIABLE]
does the same as my ruby-statement. Details are found in the online-help to read (read --help)

-----------------------------------------------------------------------


In the script I call another routine wait_for_user, which I use in my ruby-scripts to provide non-blocking keyboard-input:
Code:
require 'io/wait'
require 'io/console'

# unblocking read from STDIN

def wait_for_user()
  	char = nil
        STDIN.raw do 
                STDIN.noecho do
                        until (STDIN.ready?)
                                sleep(0.1)
                        end

                        char = (STDIN.read_nonblock(1).ord rescue nil)
                end
        end
  return char
end
The oneliner:
Code:
puts wait_for_user.chr
One exemplary use of this in a shell script (bash):
Code:
#!/bin/bash

vim -R -M -p ~/.procmail/log

echo -e "\n\tempty log (N/y)?"
resp=`readNB`

if [ "$resp" == 'y' -o "$resp" == 'Y' ]
then
  echo -e "\tdeleting log..."
  cat /dev/null > ~/.procmail/log
else
 echo -e "\tokay, doing nothing."
fi
« Prev     Main     Next »
Total Comments 4

Comments

  1. Old Comment
    I may be misunderstanding your requirements, do you really mean non-blocking, or do you mean non-line-buffered?

    If the latter, will read -r -s -N1 not serve your needs?
    Posted 10-18-2021 at 05:19 AM by GazL GazL is offline
  2. Old Comment
    Quote:
    Originally Posted by GazL View Comment
    I may be misunderstanding your requirements, do you really mean non-blocking, or do you mean non-line-buffered?
    As far as I remember, my search for a solution produced examples with non-blocking input. So I considered this the correct terminology. I understand unbuffered just as well an cannot say why I would prefer one expression over the other. This is a language problem and I will correct my blog post, when I find better translations. Thank you for pointing it out.

    Quote:
    If the latter, will read -r -s -N1 not serve your needs?
    It does. Believe it or not, I would have expected that the built-in read command were mentioned in the man-page to Bash, where it is not, and thus executed read --help for the very first time, this evening.

    Thanks again,
    Michael
    Posted 10-18-2021 at 04:15 PM by Michael Uplawski Michael Uplawski is offline
  3. Old Comment
    The non-blocking/unbuffered confusion is due to my reading English texts which only vaguely evoked the documentation that I had in German.
    Posted 10-19-2021 at 01:46 AM by Michael Uplawski Michael Uplawski is offline
  4. Old Comment
    No worries on the language, I just wanted to confirm what it was you were actually looking for.

    Within the context of the tty/terminal driver you commonly see it referred to a 'raw' vs 'cooked' input rather than the more verbose "non-line-buffered" I used.
    Posted 10-19-2021 at 06:27 AM by GazL GazL is offline
 

  



All times are GMT -5. The time now is 01:23 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration