我是一名JavaScript / Java程序员,编写bash脚本时遇到困难。以下是我要处理的输出:
private int price;
public int Price
{
get { return price/100m; }
set { price = value; }
}
我希望获得karl@karl-laptop:~/.scripts/startup$ xinput list
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ Logitech USB Receiver id=10 [slave pointer (2)]
⎜ ↳ Logitech USB Receiver id=11 [slave pointer (2)]
⎜ ↳ PS/2 Logitech Wheel Mouse id=15 [slave pointer (2)]
⎜ ↳ SteelSeries Sensei Raw Gaming Mouse id=16 [slave pointer (2)]
⎜ ↳ SteelSeries Sensei Raw Gaming Mouse id=17 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Video Bus id=8 [slave keyboard (3)]
↳ Sleep Button id=9 [slave keyboard (3)]
↳ USB2.0 UVC HD Webcam id=12 [slave keyboard (3)]
↳ Asus WMI hotkeys id=13 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=14 [slave keyboard (3)]
↳ SteelSeries Sensei Raw Gaming Mouse id=18 [slave keyboard (3)]
Virtual core pointer
的身份证明。这意味着我希望最终得到id:
SteelSeries Sensei Raw Gaming Mouse
最好在他们自己的变量中,因为我将以不同的方式编辑两者的设置。我目前有以下脚本,但它没有做任何事情:
16
17
我尝试在这个问题中使用以下脚本,但它没有起作用,它只是回应所有id:How to make a program that finds id's of xinput devices and sets xinput some settings
我确实对其进行了修改,以便收集与#!/bin/bash
ids=$(xinput list | awk 'SteelSeries Sensei Raw Gaming Mouse')
echo $ids[0]
echo $ids[1]
unset $ids
匹配的所有ID:
SteelSeries Sensei Raw Gaming Mouse
输出:
ids=$(xinput --list | awk -v search='SteelSeries Sensei Raw Gaming Mouse' \
'$0 ~ search {match($0, /id=[0-9]+/);\
if (RSTART) \
print substr($0, RSTART+3, RLENGTH-3)\
}'\
)
for i in $ids
do
echo $i
done
echo $ids[0]
echo $ids[1]
然而,它还收集了我想要的16
17
18 // <-- wrong! should not be there
16 17 18[0] // <-- I only wanted the number '16' here
16 17 18[1] // <-- I only wanted the number '17' here
(你可以看到18号)
答案 0 :(得分:1)
以下是提取您感兴趣的两个ID的快速解决方案:
xinput --list | awk '/SteelSeries Sensei .*pointer/ {print $8}' | sed 's/id=\(.*\)/\1/'