V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
hicdn
V2EX  ›  问与答

shell 脚本中调用 adb 出现的奇怪显现象

  •  1
     
  •   hicdn · 2022-11-11 19:16:13 +08:00 · 1229 次点击
    这是一个创建于 531 天前的主题,其中的信息可能已经有所发展或是发生改变。

    注释掉 adb 那行,一切正常;

    有 adb 那行,外层循环只执行一次。

    排查离 bug 一整天,最终确定如下最小复现代码。实际业务是 adb 后面还有别的操作,内层循环检查状态。

    ➜  /tmp cat tt.sh
    while read -r current
    do
        # adb shell input tap 1 2
        n=3
        while [ $n -gt 0 ]
        do
            echo $current $n
            n=$((n-1))
            [ $n -eq 1 ] && break
        done
        echo $current done
    done < <(echo aa bb cc | tr ' ' '\n')
    ➜  /tmp bash tt.sh
    aa 3
    aa 2
    aa done
    bb 3
    bb 2
    bb done
    cc 3
    cc 2
    cc done
    ➜  /tmp cat tt.sh
    while read -r current
    do
        adb shell input tap 1 2
        n=3
        while [ $n -gt 0 ]
        do
            echo $current $n
            n=$((n-1))
            [ $n -eq 1 ] && break
        done
        echo $current done
    done < <(echo aa bb cc | tr ' ' '\n')
    ➜  /tmp bash tt.sh
    aa 3
    aa 2
    aa done
    ➜  /tmp
    
    第 1 条附言  ·  2022-11-11 20:31:36 +08:00
    答案找到了,https://stackoverflow.com/questions/44634478/why-does-adb-commands-break-a-bash-script-loop
    adb shell 执行时会读 stdin 。
    用不读 stdin 的 adb exec-out 或者 adb shell -n 来替代 adb shell
    3 条回复    2022-11-11 21:00:48 +08:00
    Senorsen
        1
    Senorsen  
       2022-11-11 20:03:53 +08:00
    先无脑 set -x 看看输出
    amrnxcdt
        2
    amrnxcdt  
       2022-11-11 20:36:09 +08:00
    试一下包裹下 adb:$(adb shell input tap 1 2)
    rrfeng
        3
    rrfeng  
       2022-11-11 21:00:48 +08:00
    < /dev/null 也行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3233 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:45 · PVG 18:45 · LAX 03:45 · JFK 06:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.