2011-02-05 76 views
2

我想用一个脚本在启动过程中禁用我的触摸板这样从命令响应中提取数据,并将其存储在一个变量

#!/bin/bash

# determine device id 
ID=$(xinput list | grep -i touchpad) 

# check output 
echo $ID 

# disable device identified by $ID 
#xinput set-prop $ID "Device Enabled" 0</code> 

Basically I would like to extract "12" (or whatever number the device has) from the result of command:

  • xinput list | grep -i touchpad
    ⎜ ↳ SynPS/2 Synaptics TouchPad id=12 [slave pointer (2)]

and store it in variable $ID.

The next command would disable the device.

Any suggestions on how I can achieve that?

Thanks, Udo

回答

1

If you know the output of xinput list将始终具有ID号作为第5场,然后使用:

ID=$(xinput list | awk -F'[= ]' '/TouchPad/{print $5}') 

如果你宁愿键关机字id=使得它可以在任何地方就行了,然后使用:

ID=$(xinput list | sed '/TouchPad/s/^.*id=\([0-9]*\).*$/\1/') 
+0

XINPUT名单| awk -F'[=]''/ TouchPad/{print $ 5}'输出SynPS/2 - >更改参数不会给我12 ...第二个命令在小编辑后工作;) – udo 2011-02-05 19:51:12

+0

@udo我拒绝你的编辑,但同时有人批准它。最好使用最终使用的代码编辑您的问题,而不是修改/编辑答案。 – 2011-02-05 19:58:44

1

GNU grep

ID=$(xinput list | grep -Poi '(?<=touchpad[[:blank:]]*id=)[0-9]+') 

GNU sed

ID=$(xinput list | sed -n 's/.*touchpad[[:blank:]]*id=\([0-9]\+\)[[:blank:]]*.*/\1/Ip') 
相关问题