从命令响应中提取数据并将其存储在变量中

时间:2021-04-04 20:35:53

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:

基本上我想从命令结果中提取“12”(或设备具有的任何数字):

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

and store it in variable $ID.

并将其存储在变量$ ID中。

The next command would disable the device.

下一个命令将禁用该设备。

Any suggestions on how I can achieve that?

关于如何实现这一目标的任何建议?

Thanks, Udo

谢谢,Udo

2 个解决方案

#1


1  

If you know the output of xinput list will always have the ID number as the 5th field then use:

如果你知道xinput列表的输出总是将ID号作为第5个字段,那么使用:

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 =这样它可以在线上的任何地方,那么使用:

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

#2


1  

GNU grep:

GNU grep:

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

GNU sed:

GNU sed:

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

#1


1  

If you know the output of xinput list will always have the ID number as the 5th field then use:

如果你知道xinput列表的输出总是将ID号作为第5个字段,那么使用:

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 =这样它可以在线上的任何地方,那么使用:

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

#2


1  

GNU grep:

GNU grep:

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

GNU sed:

GNU sed:

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