read single chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read single chars
# 1  
Old 08-25-2010
read single chars

This reads single keystrokes and produces an output:

Code:
#! /bin/bash

while : ; do
    read -s -n 1 >/dev/null 2>&1
    echo ${REPLY}
done | awk '{print}'

This second one don't. Even though these examples make no sense; the real code is more complicated. Who knows what the problem is here? Something with STDIN and STDOUT?

Code:
#! /bin/bash

while : ; do
    read -s -n 1 >/dev/null 2>&1
    echo ${REPLY}
done | awk '{print}' | while read x ; do echo ${x} ; done

# 2  
Old 08-25-2010
This is because your input never gets terminated. Meanwhile awk is buffering the output. Watch what happens when you replace the loop by:

Code:
while read -s -n 1 >/dev/null 2>&1; do
  case $REPLY in 
     "z") break
  esac
  echo ${REPLY}
done | awk '{print}' | while read x ; do echo ${x} ; done

Now type input characters and then enter a z . You'll notice that the letters get printed once you enter the z, which will terminate the loop en thus the output and the buffers get flushed.

Note that you first example worked, perhaps because you were using a specific awk (gawk?) and it seems to buffer its output only when it is writing to a file or pipe. When I tried it with mawk, you first example did not work because mawk was buffering there as well.

Buffer control is not standard in awk.
When using gawk, this should produce interactive output:
Code:
gawk '{print;fflush("")}'

When using mawk:
Code:
mawk -W interactive '{print}'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-25-2010
yap - this works. Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

Read 2 input and produce it in single ouput?

Hi all. I’ve 2 inputs here and would like to produce it in single ouput. I’ve drafted simple shell script but not sure how to put all this together. The final output should be “GROUP-XYZ” instead of “TEST” Please advise. INPUT1 GROUP-XYZ INPUT2 type8code0@box:~/dbedit$ cat... (8 Replies)
Discussion started by: type8code0
8 Replies

3. UNIX for Dummies Questions & Answers

Characters in a single read

Hi, I have the file which has the data : accctaaccctaaccctaaccctaaccctaaccctaaccctaaccctaac cctaacccaaccctaaccctaaccctaaccctaaccctaaccctaacccc taaccctaaccctaaccctaaccctaacctaaccctaaccctaaccctaa ccctaaccctaaccctaaccctaaccctaacccctaaccctaaccctaaa... (24 Replies)
Discussion started by: rajivn786
24 Replies

4. UNIX for Dummies Questions & Answers

Replacing multiple special chars with single char

Hi I've a string . And i need to replace set of characters with a single character Means .. or . or ... and so on should be replaced with single % character Irrespective of number of dots in between the characters , those should be replaced with single % All the above strings should be... (3 Replies)
Discussion started by: smile689
3 Replies

5. Shell Programming and Scripting

Read value of a file, remove first 2 chars and put this value in a variable

Hi i have need of read a file value with cat command and remove first 2character for example cat /sys/class/rtc/day 0x12 Remove char 12 And put this value in a variable is possible with a script thanks for help (6 Replies)
Discussion started by: enaud
6 Replies

6. Shell Programming and Scripting

Bash KeyPress (or Read Single Character)

Hi, I'm sorry if this has already been posted somewhere but I can't seem to find it on the forums (or anywhere on google :( ) I am writing a script where a user must enter a single character to perform an action. For example, Press Q to Quit or R to Refresh Basically I am stuggling... (5 Replies)
Discussion started by: Ste_Moore01
5 Replies

7. Shell Programming and Scripting

Read Single Value From File With Perl

Hi all, I have what I would have thought was a very simple problem but I can' find an elegant solution. I have a file which has a single value you in it, say 194. All I want my perl script to do is open the file, read the value and assign that value to a variable. I've done stuff like... (1 Reply)
Discussion started by: Donkey25
1 Replies

8. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

9. Shell Programming and Scripting

How to convert C source from 8bit chars to 16bit chars?

I was using the following bash command inside the emacs compile command to search C++ source code: grep -inr --include='*.h' --include='*.cpp' '"' * | sed "/include/d" | sed "/_T/d" | sed '/^ *\/\//d' | sed '/extern/d' Emacs will then position me in the correct file and at the correct line... (0 Replies)
Discussion started by: siegfried
0 Replies

10. Shell Programming and Scripting

Read line with a single key press...

I would really like to have a script that will accept the key press from the user with out having to press the enter key afterwards. i.e. echo "Press Y to print \c" read YesNo At this point the user has to press the enter key to continue. Is there a way to accept the key press from the... (3 Replies)
Discussion started by: jagannatha
3 Replies
Login or Register to Ask a Question