V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
hicdn
V2EX  ›  问与答

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

  •  1
     
  •   hicdn · Nov 11, 2022 · 1730 views
    This topic created in 1273 days ago, the information mentioned may be changed or developed.

    注释掉 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
    
    Supplement 1  ·  Nov 11, 2022
    答案找到了,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 replies    2022-11-11 21:00:48 +08:00
    Senorsen
        1
    Senorsen  
       Nov 11, 2022
    先无脑 set -x 看看输出
    amrnxcdt
        2
    amrnxcdt  
       Nov 11, 2022
    试一下包裹下 adb:$(adb shell input tap 1 2)
    rrfeng
        3
    rrfeng  
       Nov 11, 2022
    < /dev/null 也行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1095 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 18:03 · PVG 02:03 · LAX 11:03 · JFK 14:03
    ♥ Do have faith in what you're doing.