grep: Unknown option 'P' (see "grep --help")脚本会报这个
时间: 2024-02-11 09:03:41 浏览: 27
抱歉,这是因为 `grep` 命令在某些 Android 系统中不支持 `-P` 选项,可以使用 `sed` 命令代替。以下是修改后的脚本:
```bash
#!/system/bin/sh
# 指定需要监听的按键码
keycode="158"
# 监听 /dev/input/event0 设备文件
getevent -t /dev/input/event0 | while read line; do
code=$(echo "$line" | sed -n 's/^.*code \([0-9]\{1,\}\).*$/\1/p')
if [ "$code" = "$keycode" ]; then
echo "Button $keycode is pressed."
fi
done
```
这个脚本与之前的脚本基本一致,只是将 `grep` 命令替换成了 `sed` 命令,并且修改了正则表达式。`sed` 命令使用 `-n` 选项禁止输出所有行,然后使用正则表达式提取出按键码。你可以将 `$keycode` 变量修改为你想要监听的按键码。注意,在某些 Android 系统中,需要将这个脚本保存为可执行文件,并且需要在 root 权限下运行。
相关问题
getevent -t -c 1 /dev/input/event0 | grep -oP ".*?((?<=KEYCODE_)[A-Z0-9]+).*" | awk '{print $10}' grep: Unknown option 'P' (see "grep --help")
这是一个Linux命令,用于获取输入设备(比如触摸屏)的事件并解析出按键码。但是,命令中的正则表达式有误,导致grep命令无法正确识别选项。正确的命令应该是:
```
getevent -t -c 1 /dev/input/event0 | grep -oE ".*?((?<=KEYCODE_)[A-Z0-9]+).*" | awk '{print $10}'
```
其中,-oE选项表示使用正则表达式进行匹配,并输出匹配到的内容;正则表达式中使用了零宽度正回顾后发断言,以匹配出按键码。awk命令则是为了输出匹配到的按键码。
dumpsys window | grep -Po 'mCurrentFocus=Window{\K[^}]*(?=})' | awk -F '[ /]+' '{print $NF}' grep: Unknown option 'Po' (see "grep --help") Failed to write while dumping service window: Broken pipe
很抱歉,看来您的系统中的grep命令不支持-P和-o选项,因此上面的命令无法正常运行。您可以尝试以下命令来获取"com.tencent.start.tv/com.tencent.start.ui.SplashActivity":
```
dumpsys window | grep "mCurrentFocus" | cut -d' ' -f7 | cut -d'}' -f1
```
这个命令的作用是从"dumpsys window"命令的输出中,使用grep过滤出包含"mCurrentFocus"的行,然后使用cut命令提取出"com.tencent.start.tv/com.tencent.start.ui.SplashActivity"。这个命令将输出"com.tencent.start.tv/com.tencent.start.ui.SplashActivity"。