Extracting data from a command response and store it in a variable
I would like to disable my touchpad during startup process with a script like this
#!/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
If you know the output of xinput list
will always have the ID number as the 5th field then use:
ID=$(xinput list | awk -F'[= ]' '/TouchPad/{print $5}')
If you'd rather key off of the word id=
such that it can be anywhere on the line then use:
ID=$(xinput list | sed '/TouchPad/s/^.*id=\([0-9]*\).*$/\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')
精彩评论