开发者

Extracting data from a command response and store it in a variable

开发者 https://www.devze.com 2023-02-08 13:52 出处:网络
I would like to disable my touchpad during startup process with a script like this #!/bin/bash # determine device id

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:

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')
0

精彩评论

暂无评论...
验证码 换一张
取 消