在不离开 bash 脚本的情况下中断 logcat?

Interrupt logcat without leaving the bash script?

我写了一个基本的 shell 脚本,我打算用它来减少测试我的设备所需的时间。

我当前的问题是当我想停止执行 logcat 时,如果我使用标准 Ctrl + C 命令,我杀死了整个脚本。 我只希望脚本循环回到选项菜单。

我确实需要 logcat 来持续显示数据,所以 logcat -d 不是一个选项。

这个问题有解决办法吗?

谢谢。

代码如下:

#!/bin/bash

PS3='Select an option: '
options=("Restart ADB Server" "ABR Test" "Reboot the device" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Restart ADB Server")
            printf "\nRestarting the ADB Server...\n"
            adb disconnect
            adb kill-server
            adb start-server
            adb connect 192.168.1.100
            ;;
        "ABR Test")
            printf "\nStarting the ABR Test\n"
            clear
            adb logcat | grep onVideoInputFormatChanged --line-buffered
            ;;
        "Reboot the device")
            printf "\nRebooting the device...\n"
        adb reboot
            ;;
        "Quit")
            break
            ;;
        *)  printf "\n$REPLY is an invalid option!\n";;
    esac
done

您可以尝试使用 [=12= 捕捉 Ctrl + C (SIGINT) ] 关键字 (trap <callback> <signal>) 并从回调函数中停止 logcat,如下所示:

stop_logcat() {
    # stop logcat here
}

trap 'stop_logcat' SIGINT

# your code here

关于 Traps 的文档。