Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is an exact duplicate of:

My laptop keyboard is broke (Esc keeps randomly being pressed, can't fix it), so I've bought a USB keyboard instead. I want to write a script that will run at login which will check if my USB keyboard is connected, and disable my laptop keyboard if so.

I want to use xinput to do this. for background, the correct command to disable device with ID 14 would be:

xinput set-int-prop 14 "Device Enabled" 8 0

I have what I thought was a reliable method of making sure that I am disabling the correct device by listing, grepping, cutting to get the ID number, and then using it in the correct command like so:

xinput --list | egrep "AT Translated" | cut -d "=" -f 2| cut -d "[" -f 1 | xargs -I {} xinput set-int-prop {} "Device Enabled" 8 0

However even this gives a fault:

unable to find device 14

I'm not sure why that is (first question), but I was wondering how I'd go about checking to see if the USB keyboard is there? I need an if function obviously, something which checks if any result is returned when I grep the string "USB USB Keykoard" [yes that spelling is wrong but what appears on my list] from xinput --list. So my second question is, how do I write that if function?

share|improve this question

marked as duplicate by jasonwryan, Renan, rahmu, manatwork, Thor Feb 15 '13 at 8:25

This question was marked as an exact duplicate of an existing question.

What's about udev script? I did so with USB-mouse. – Eddy_Em Feb 14 '13 at 17:22
Please tell me a little more, I haven't used udev before. The method I use above is only because I understand it! – user5866 Feb 14 '13 at 17:34
That's quite simple: you need to detect actions of attaching and detaching of USB-keyboard, and run your scripts on them. My example for mouse: ACTION=="add", SUBSYSTEM=="input", ENV{INPUT_MOUSE}="1", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/eddy/.Xauthority", RUN+="/usr/bin/synclient TouchpadOff=1" ACTION=="remove", SUBSYSTEM=="input", ENV{ID_INPUT_MOUSE}="1", ENV{DISPLAY}=":0.0", ENV{XAUTHORITY}="/home/eddy/.Xauthority", RUN+="/usr/bin/synclient TouchpadOff=0" – Eddy_Em Feb 14 '13 at 17:47
its a fair point... but even with Eddy_Em's (good) asnwer, I still can't do it as i'm too much of a novice. I've been googling udev without much success... – user5866 Feb 14 '13 at 21:06

I suspect the problem with your shell command is here:

| cut -d "[" -f 1

This leaves you with an argument of 14 (the number 14 followed by several spaces; note that markdown isn't displaying it for some reason). Since the argument that xinput is looking for is 14 instead of 14 (14 plus several spaces), the command fails.

Instead, use the space instead of the [ as the delimiter in this section:

| cut -d " " -f 1
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.