pipe bug question

If none of the more specific forums is the right place to ask

pipe bug question

Postby paratrap » 2020-06-26 12:14

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.
paratrap
 
Posts: 35
Joined: 2010-09-05 13:08

Re: pipe bug question

Postby LE_746F6D617A7A69 » 2020-06-26 17:50

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
LE_746F6D617A7A69
 
Posts: 387
Joined: 2020-05-03 14:16

Re: pipe bug question

Postby paratrap » 2020-06-26 22:04

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)]
paratrap
 
Posts: 35
Joined: 2010-09-05 13:08

Re: pipe bug question

Postby LE_746F6D617A7A69 » 2020-06-27 11:14

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
LE_746F6D617A7A69
 
Posts: 387
Joined: 2020-05-03 14:16

Re: pipe bug question

Postby paratrap » 2020-06-27 14:09

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
paratrap
 
Posts: 35
Joined: 2010-09-05 13:08

Re: pipe bug question

Postby Head_on_a_Stick » 2020-06-27 14:38

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
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: pipe bug question

Postby paratrap » 2020-06-27 14:55

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) !
paratrap
 
Posts: 35
Joined: 2010-09-05 13:08

Re: pipe bug question

Postby LE_746F6D617A7A69 » 2020-06-27 15:14

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
LE_746F6D617A7A69
 
Posts: 387
Joined: 2020-05-03 14:16

Re: pipe bug question

Postby Head_on_a_Stick » 2020-06-27 15:23

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
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: pipe bug question

Postby paratrap » 2020-06-27 20:32

Wow! Thank you very match. This is actually a solution. Indeed awk does not fflush! How did I never run into this issue yet!!!
paratrap
 
Posts: 35
Joined: 2010-09-05 13:08

Re: pipe bug question

Postby Head_on_a_Stick » 2020-06-28 08:04

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.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair


Return to General Questions

Who is online

Users browsing this forum: No registered users and 18 guests

fashionable