Input buffer and READ


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Input buffer and READ
# 1  
Old 03-12-2013
Input buffer and READ

Hello everyone,
Can someone please explain the input buffer behaviour for the read command in ksh93 on AIX?
I have 'googled' for weeks now, and did not find a satisfactory answer or solution to my dilemma.
I have the following code:
Code:
STTY=$(stty -g)
if [ -t 0 ];then 
   stty -echo -icanon time 0 min 0
fi
k=""
while [[ "x$k" = "x" ]];do
read k

# here is a group of command that retrieve data from several files
# and process the data summarizing and filtering it (using awk).
# This section could possibly take 1 to 2 seconds to complete its job
# the final command does a cat of the generated data file to the screen
# then the loop would start again retrieving fresh stats from files....
#  ........
done
stty $STTY

How can I guarantee that the input buffer to the read will keep a key that the user presses while inside the loop execution?
When I execute the code and press any key, sometimes it exits properly, other times it takes more than one key press to exit, maybe because the key got pressed while the processing section of the loop was running.
What could be 'chewing up' the pressed keystroke? Did the input buffer clear?
Do you have suggestions on how to guarantee termination on a single key stroke?
Maybe I need to redesign my loop logic ?
I am open to any advice and suggestion.
Thanks to all!
# 2  
Old 03-12-2013
read is known to alter terminal settings in many shells. To guarantee you're getting a raw read with the terminal characteristics you set, try dd.

Code:
k="$(dd count=1)"

Also note that some keypresses may be more than one byte of output.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Barcode scanner buffer read manipulation

Hello, I'm facing some problems with a barcode scanner from Cygnal Inc, model Sweda SL-20. Info from lsusb -v: Bus 003 Device 003: ID 10c4:ff11 Cygnal Integrated Products, Inc. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 1.10 ... (4 Replies)
Discussion started by: minharojr
4 Replies

2. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

3. Programming

Clear standard input buffer for C program in Linux

Hello friends! i am writing a code in which i take inputs (numbers) from user and count the total number of positive, negative and zeros entered. I need to clear my standard input buffer before scanf() command. My compiler is completely ignoring the fflush(stdin) command. its not even showing any... (1 Reply)
Discussion started by: Abhishek_kumar
1 Replies

4. Programming

Separate input buffer into multiple lines

Hi All I have a very tricky problem, not able to solve it. Hence asking this question. I have a code portion like this - int parse_msg(in_buf,line1,line2,sccs_line) char in_buf; char line1; char line2; char sccs_line; { .... (void)fprintf(trace_fp,"parse_msg1 in_buf = %s \n",... (0 Replies)
Discussion started by: nsinha
0 Replies

5. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. Programming

Maximum buffer size for read()

Hi friends, Hope everybody is fine. First have a look at my code, then we will talk about it. $ cat copy.c #include <stdio.h> #define PERMS 0644 /* RW for owner, R for group, others */ #define BUFSIZE 1 char *progname; int main(int argc,char * argv) { int f1, f2, n; ... (4 Replies)
Discussion started by: gabam
4 Replies

8. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

9. Shell Programming and Scripting

Expect: How to read buffer for spawn

Hi All, I have to write one expect script to login to one system. I am using set timeout -1 match_max 100000 spawn ssh root@hostname Now when I do spawn ssh to that host it send some warning message and one challenge Challenge: 111-2345 I need to read this challenge value and has... (1 Reply)
Discussion started by: Lokesh Agrawal
1 Replies

10. Shell Programming and Scripting

Increase the buffer size to read lengthy lines

Hi All, I am trying to read output from a command. The output format is as follows: Thursday 13 Mar 2008 Information This is sample text Friday 14 Mar 2008 Warning This is one more sample text First line contains informtation (date etc) and the 2nd line contains some information. ... (3 Replies)
Discussion started by: ssunda6
3 Replies
Login or Register to Ask a Question