2011-02-05 42 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') 
相關問題