0
sudo dmesg -w|grep "Manufacturer: Keychron K1"| xargs -I{} xset r rate 250 70

It does not work, why?

I am trying to reset keyboard settings when the keyboard is reconnected, but I cannot get dmesg -w|xargs... combination to work. It supposed to be working, for example if I do this:

while :; do echo tick; sleep 1s; done|xargs -I{} date

I will get a new time printed every second. The idea was to use dmesg -w then grep then xargs but it does not work until xargs is killed from another terminal.

Why and how to fix?

exebook
  • 32,014
  • 33
  • 141
  • 226
  • Ok I used `ag` instead of `grep` and it worked! Must be grep bufferring issue, maybe it even is controllable by an option. – exebook Jan 20 '22 at 18:04
  • That's neither the problem you have to solve, nor a solution for what you actually need to do. Please don't implement a ["spacebar heater" / XKCD 1172](https://xkcd.com/1172/). – datenwolf Jan 20 '22 at 18:12
  • but... why not configure your desktop manager or X-server settings to do configure your keyboard?... – KamilCuk Jan 20 '22 at 19:47

2 Answers2

0

You're asking an XY(Z) problem here, see "What is the XY problem?".

Your problem X is "how do I automatically have input devices configured in Xorg when hotplugged?"

And for that you simply want to write a xorg.conf.d InputClass rule that will make the appropriate settings for your keyboard:

https://www.x.org/releases/current/doc/man/man5/xorg.conf.5.xhtml#heading9


However you misidentified this problem as problem Y "how to automatically execute a program upon hotplug?" – for that we have UDev

For that problem look at UDev rules: https://wiki.archlinux.org/title/Udev


However misidentified problem Y as the "problem" Z "how can I execute the last part of a chain if pipe redirections, when a certain string appears". Tackling that does not solve your actual problem.

This

sudo dmesg -w | grep … | xargs …

pipes three programs together, which are all executing at the same time. xargs waits for the end of input and then executes whatever was passed to it as parameters. Of course dmesg -w will never produce an end-of-file.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
0

Why

Buffering.

how to fix?

Set line buffering of tools with stdbuf or grep --line-buffered.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111