【问题标题】:adb push/pull with progress bar带进度条的 adb 推/拉
【发布时间】:2011-09-29 12:53:35
【问题描述】:

如果您将大文件推送/拉到设备上,这真的很烦人,现在无法知道它有多远。是否可以运行 adb push 或 adb pull 并使用“bar”实用程序获取进度条?

这里的主要问题是我认为 adb 需要两个文件名,如果输入文件可以被 stdin 替换,您可以通过 'bar' 实用程序管道并获得一个进度条。到目前为止,我还没有成功,但我并不是真正的 shell 大师,这就是我在这里问的原因:)

请注意,我在 Linux 上使用 bash。

【问题讨论】:

    标签: android linux progress-bar adb


    【解决方案1】:

    好吧,我可以给你一个想法:

    ADB_TRACE=adb adb push <source> <destination> 
    

    返回任何命令的日志,例如复制命令,如下所示:

    writex: fd=3 len=65544: 4441544100000100000000021efd  DATA....#....b..
    

    在这里您可以使用 ls -a 获取之前的总字节长度,然后使用 grep 或 awk 解析 adb 的输出,增加一个内部计数器并将当前进度发送到 bar 实用程序。

    成功后,请在此处发布脚本。

    【讨论】:

    • 酷,我不知道这是可能的!
    • 看看 adb --help 的输出,很有用
    • 这需要做成一个完整的脚本。
    • 在下面查看我的答案,我已经发布了一个简单的脚本。
    【解决方案2】:

    目前我有这么一小块 bash:

    function adb_push {
      # NOTE: 65544 is the max size adb seems to transfer in one go
      TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
      exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
      # start bar in the background
      ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
      echo  # Add a newline after the progressbar.
    }
    

    它有点工作,它显示了一个从 0 到 100 的进度条,这很好。但是,如果你做很多小文件,那就不正确了,更糟糕的是,'bar'显示的字节数和总字节数都不正确。

    我挑战你改进我的剧本;应该不难! ;)

    【讨论】:

    • bar -s line 316: no such file 崩溃。
    • @chx sudo apt-get install bar? (bar 是包含命令 bar 的 Debian 软件包。)
    • unbuffer 来自 expect 包。要在 debian 上安装,请键入 sudo apt install expect
    【解决方案3】:

    QtADB 有每个文件和总进度条。 http://qtadb.wordpress.com/

    【讨论】:

    • 启动时出现分段错误。
    • @nuclearmistake 该站点上最新版本的 Windows 下载链接已损坏。
    【解决方案4】:

    我为 adb 做了一个补丁来解决这个问题。

    https://android-review.googlesource.com/#/c/87748

    【讨论】:

    • 它介绍了什么:“为输出文件传输进度(传输的字节数、总字节数和完成百分比)的 'push' 和 'pull' 命令添加了一个新的 '-p' 开关。这在传输大文件时提供有用的反馈,还可以让其他工具轻松监控分叉的推/拉命令的进度。”
    【解决方案5】:

    我发现了一个不完整但很接近的 Python pastebin:它需要一个 \r 和参数支持。

    【讨论】:

      【解决方案6】:

      这是我的解决方案,它将显示一个简单的进度条和当前的数字进度

      [==================================================] 100%
      

      用法

      ./progress_adb.sh source destination
      

      progress_adb.sh

      #!/bin/bash
      
      function usage()
      {
          echo "$0 source destination"
          exit 1
      }
      
      function progressbar()
      {
          bar="=================================================="
          barlength=${#bar}
          n=$(($1*barlength/100))
          printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
          # echo -ne "\b$1"
      }
      
      export -f progressbar
      
      [[ $# < 2 ]] && usage
      
      SRC=$1
      DST=$2
      
      [ ! -f $SRC ] && { \
          echo "source file not found"; \
          exit 2; \
      }
      
      which adb >/dev/null 2>&1 || { \
          echo "adb doesn't exist in your path"; \
          exit 3; \
      }
      
      SIZE=$(ls -l $SRC | awk '{print $5}')
      ADB_TRACE=adb adb push $SRC $DST 2>&1 \
          | sed -n '/DATA/p' \
          | awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
      
      echo 
      

      在 Ubuntu 14.04 上测试

      $ bash --version
      GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
      

      待办事项

      目录支持

      屏幕大小改变时进度条大小改变

      【讨论】:

        【解决方案7】:

        最新的 adb 好像有进度支持了。

        Android Debug Bridge version 1.0.32
        device commands:
        adb push [-p] <local> <remote>
            - copy file/dir to device
            ('-p' to display the transfer progress)
        

        但是,上面的答案也适用于没有进度选项的“adb install”。我修改了第一个答案的脚本以这种方式工作:

        在 PATH 中的某处创建“adb-install.sh”并运行“adb-install.sh”而不是“adb install -f”

        #!/bin/bash
        # adb install with progressbar displayed
        # usage: <adb-install.sh> <file.apk>
        # original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar
        
        function usage()
        {
            echo "$0 <apk to install>"
            exit 1
        }
        
        function progressbar()
        {
            bar="================================================================================"
            barlength=${#bar}
            n=$(($1*barlength/100))
            printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
            # echo -ne "\b$1"
        }
        
        export -f progressbar
        
        [[ $# < 1 ]] && usage
        
        SRC=$1
        
        [ ! -f $SRC ] && { \
            echo "source file not found"; \
            exit 2; \
        }
        
        which adb >/dev/null 2>&1 || { \
            echo "adb doesn't exist in your path"; \
            exit 3; \
        }
        
        SIZE=$(ls -l $SRC | awk '{print $5}')
        export ADB_TRACE=all
        
        adb install -r $SRC 2>&1 \
            | sed -n '/DATA/p' \
            | awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
        
        export ADB_TRACE=
        
        echo
        
        echo 'press any key'
        read n
        

        【讨论】:

          猜你喜欢
          • 2015-10-31
          • 1970-01-01
          • 2014-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          • 2023-02-23
          • 2017-07-27
          相关资源
          最近更新 更多