Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

pipe bug question

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
paratrap
Posts: 47
Joined: 2010-09-05 13:08

pipe bug question

#1 Post by paratrap »

Hello!

I'm trying to read from 'xinput test 14' to variable and got very strange behaviour. Can anyone explain why:

Code: Select all

xinput test 14 | awk '{print $3}'
outputs (when pressed ABC on keyboard):

Code: Select all

38
56
54
and:

Code: Select all

xinput test 14 | awk '{print $3}' | cat
outputs nothing. Yet at the same time:

Code: Select all

echo ok | awk '{print $1}' | cat
works as expected. BTW 'xinput test 14' - 14 - main keyboard.

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 65 times

Re: pipe bug question

#2 Post by LE_746F6D617A7A69 »

Strange. For Me it works like this: (pressed a, b, c, then ctrl-c)

Code: Select all

$> xinput test 10 | awk '{print $3}' | cat
abc^C
and this:

Code: Select all

$> xinput test 10 | awk '{print $1}' | cat
abc^C
It looks like xinput is printing directly to terminal somehow, not to stdout -> awk gets nothing, and so nothing is passed to cat
The echo is printing to stdout, so the first pipe works as expected.

As you can see, on my system xinput is printing the letters, not keycodes. (Debian10 amd64, xfce-terminal)
So the question is: what system You're using and which terminal emulator? Do You use standard xorg config?
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

paratrap
Posts: 47
Joined: 2010-09-05 13:08

Re: pipe bug question

#3 Post by paratrap »

I'm using Debian 10 + lightdm + gnome-terminal.

To be exact first output of 'xinput test 14 | awk '{print $3}'' is:
a38
56
b56
54
c54

Code: Select all

$ xinput test 14 | awk '{print $3}' | cat 
abc
and second output is 'abc' because I add 'cat' to the end. Which is very wired behavior. Like someone changing IFS or other environment variables (witch I checked - everything is ok) or deliberately changing output pipe number. I do not understand what is going on.

And full output of 'xinput test 14' is:

Code: Select all

key press   38 
akey release 38 
key press   56 
bkey release 56 
key press   54 
ckey release 54 

Code: Select all

xinput list
⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ KYE SYSTEMS CORP. Wired Mouse           	id=9	[slave  pointer  (2)]
⎜   ↳ ELAN1301:00 04F3:3115 Mouse             	id=11	[slave  pointer  (2)]
⎜   ↳ ELAN1301:00 04F3:3115 Touchpad          	id=12	[slave  pointer  (2)]
⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
    ↳ Power Button                            	id=6	[slave  keyboard (3)]
    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
    ↳ Power Button                            	id=8	[slave  keyboard (3)]
    ↳ USB2.0 HD UVC WebCam: USB2.0 HD         	id=10	[slave  keyboard (3)]
    ↳ Asus WMI hotkeys                        	id=13	[slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard            	id=14	[slave  keyboard (3)]

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 65 times

Re: pipe bug question

#4 Post by LE_746F6D617A7A69 »

I don't have the "AT Translated Set 2 keyboard" on the list, and for other keyboard devices xinput just prints the letters, not events.
However, it works with the mouse device:

Code: Select all

xinput --test 8
motion a[0]=447 
motion a[1]=932 
motion a[0]=447 
^C
I have checked that the redirection works at the awk program level:

Code: Select all

xinput --test 8 | awk '{print $2 | "cat -"}'
a[0]=607
a[0]=606
a[1]=898
a[1]=899
a[0]=605
So, it seems that the problem is related to awk

But anyway, this is just another example of UUOC :D
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

paratrap
Posts: 47
Joined: 2010-09-05 13:08

Re: pipe bug question

#5 Post by paratrap »

Nah. It wasn't about 'cat award' actual code is:

Code: Select all

while read -r LINE; do echo $LINE; done < <(xinput test 14 | awk '{print $3}')
I have same issue with '< <()' or ' | cat'. You saying it is awk issue, but I gave an example which works:

Code: Select all

echo ok | awk '{print $1}' | cat
So, I still have no idea why I can't ' < <()' xinput test + awk

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: pipe bug question

#6 Post by Head_on_a_Stick »

paratrap wrote:actual code is
You really should have put that in the OP...

Code: Select all

while read -r line ; do echo ${line##* } ; done < <(xinput test 14)
I think your "pipe bug" is due to the fact that bash runs the last element of the pipe in a subshell: http://mywiki.wooledge.org/BashFAQ/024
deadbang

paratrap
Posts: 47
Joined: 2010-09-05 13:08

Re: pipe bug question

#7 Post by paratrap »

I do not know how to explain this better then I already did. This line works:

Code: Select all

xinput test 14 | awk '{print $3}'
This line gives no output (except bash echo's symbols your type):

Code: Select all

xinput test 14 | awk '{print $3}' | cat
sub shell is not an issue here. Just regular bash commands. If you could confirm those lines works on your machine or they don't it would help a lot (do not forget to update keyboard id == 14) !

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 65 times

Re: pipe bug question

#8 Post by LE_746F6D617A7A69 »

Using only bash built-ins:

Code: Select all

while read -a line; do echo ${line[1]}; unset line; done < <(xinput test 8)
What it does:
1. Read line from xinput into a string array, using default IFS
2. Print 2nd word from the array (indexes are zero-based)
3. Clear the array
4. goto 1
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: pipe bug question

#9 Post by Head_on_a_Stick »

paratrap wrote:sub shell is not an issue here
Yes, I was wrong about that. My apologies.

Tomazzi is right, it's an awk issue:

Code: Select all

xinput test 14 | awk '{print $3;fflush(stdout)}' | cat
See also https://superuser.com/questions/379122/ ... utput-file
deadbang

paratrap
Posts: 47
Joined: 2010-09-05 13:08

Re: pipe bug question

#10 Post by paratrap »

Wow! Thank you very match. This is actually a solution. Indeed awk does not fflush! How did I never run into this issue yet!!!

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: pipe bug question

#11 Post by Head_on_a_Stick »

paratrap wrote:This is actually a solution
Well not really, awk isn't needed at all — just use either a bash array or parameter expansion (which can be used with any Bourne shell) to extract the information.

Anyway, please mark the thread [SOLVED] for the benefit of others. Thanks.
deadbang

Post Reply