Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to write a script to disable keyboard. I write xinput disable 13 as built-in keyboard's id = 13. But what if the 'id' changes somehow? Then it won't work anymore. That's why I have to write better script, the one that uses grep.

For now, I got this

xinput list|grep AT|grep id=
↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]

I added cut -f 2 to my command, so it displays this > id=13 So, for now, I got 'id=13', but I need only 13, without id=, so I could assign that value to some variable, to pass it to xinput disable $variable

Can someone help me to complete my script?

share|improve this question
add comment

4 Answers

This should work for you.

xinput list|grep AT|grep id=|cut -f 2|cut -f 2 -d =

cut command has an option to specify the delimiter.

xinput list|grep AT|grep id=|cut -f 2

This gives you id=13, so pass it to another cut command specifying the delimiter as =

Sample

~$ xinput 
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Dell Premium USB Optical Mouse            id=10   [slave  pointer  (2)]
⎜   ↳ DualPoint Stick                           id=15   [slave  pointer  (2)]
⎜   ↳ AlpsPS/2 ALPS DualPoint TouchPad          id=16   [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)]
    ↳ Sleep Button                              id=9    [slave  keyboard (3)]
    ↳ Dell Dell USB Keyboard Hub                id=11   [slave  keyboard (3)]
    ↳ Dell Dell USB Keyboard Hub                id=12   [slave  keyboard (3)]
    ↳ Laptop_Integrated_Webcam_FHD              id=13   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=14   [slave  keyboard (3)]
    ↳ Dell WMI hotkeys                          id=17   [slave  keyboard (3)]
~$ xinput list|grep AT|grep id=|cut -f 2|cut -f 2 -d =
14
share|improve this answer
    
Thanks! It works. –  alexa May 5 '13 at 12:02
    
Please accept the answer, if it works for you :) –  thefourtheye May 5 '13 at 12:03
add comment

You do not have to specify a numeric index, the following commands suffices:

xinput --disable 'AT Translated Set 2 keyboard'

Have a look at the xinput manual page for more recipes.


If you want to parse the ID using grep, you could use the following:

$ xinput list | grep -Po 'AT.*id=\K[0-9]+'
13

For more details about \K, see Can grep output only specified groupings that match?.

share|improve this answer
add comment

So you want a substring?

string=$(xinput | grep HID | cut -f 2);
echo ${string:3}

This does not work in shell only in bash

chad@M4A79XTD-EVO:~$ xinput 
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ HID 04b4:0033                             id=8    [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)]
    ↳ Power Button                              id=7    [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=9    [slave  keyboard (3)]
chad@M4A79XTD-EVO:~$ string=$(xinput | grep HID | cut -f 2);echo ${string:3}
8
share|improve this answer
    
Thanks! It works too. –  alexa May 5 '13 at 12:02
add comment

Even if you can use grep for this, I think you would be better off with sed here:

xinput list | sed -nr 's/.*AT.*keyboard.*id=([0-9]*).*/\1/p'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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