仅从xinput -list获取设备名称

时间:2016-09-19 17:14:49

标签: bash

我已经编写了一个小程序来切换设备是否已启用,主要是从热键运行以启用/禁用触摸板,因为我的手总是在打字时点击它。 我使用notify-send来创建一个对话框,但我目前所做的只是说

device $1 has been enabled

xinput列表的输出如下:

⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Atmel Atmel maXTouch Digitizer            id=10   [slave  pointer  (2)]
⎜   ↳ ETPS/2 Elantech Touchpad                  id=14   [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)]
    ↳ USB2.0 HD UVC WebCam                      id=11   [slave  keyboard (3)]
    ↳ Asus WMI hotkeys                          id=12   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]

有可靠的方法可靠地获取ETPS/2 Elantech TouchpadUSB2.0 HD UVC WebCam吗?一个正则表达式不能正确匹配,除非我找到↳字符,介于两者之间,然后是多个空格,但这是正则表达式的有效字符吗?

编辑:脚本非常简单,所以我已将其包含在内,并使用设备编号进行调用(如果我还可以toggle_device touchpad会很酷,但这是后来的问题)

#!/bin/bash 

DEVICE_ENABLED=`xinput list-props $1 | grep "Enabled" | awk '{print $NF}'`

if [ "$DEVICE_ENABLED" == "1" ] #disable if it's enabled 
    then
    xinput set-prop $1 "Device Enabled" 0
    notify-send "Device $1 has been disabled"
else
    xinput set-prop $1 "Device Enabled" 1
    notify-send "Device $1 has been enabled"
fi

1 个答案:

答案 0 :(得分:1)

在实践中,是的,你可以这样做。

arrow=$'\xe2\x86\xb3'
while IFS= read -r line; do
  if [[ $line = *"$arrow"* ]]; then
    content=${line#*"$arrow"}  # delete everything before the arrow
    content=${line%%$'\t'*}    # delete everything after first tab
    echo "Found content: $content"
  fi
done < <(xinput --list)

也就是说,调用xinput --list --name-only更容易,而不需要过滤。