带进度条的adb推/拉

dar*_*ngs 34 linux android adb progress-bar

如果您将大文件推送/拉到设备,那么现在无处可去,这真的很烦人.是否可以使用"bar"实用程序运行adb push或adb pull并获取进度条?

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

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

jpa*_*500 28

看起来最新的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)
Run Code Online (Sandbox Code Playgroud)

但是,上面的答案也适用于没有进度选项的"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
Run Code Online (Sandbox Code Playgroud)


dar*_*ngs 14

目前我有这个小小的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.
}
Run Code Online (Sandbox Code Playgroud)

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

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


小智 9

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

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


ali*_*dro 8

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

[==================================================] 100%
Run Code Online (Sandbox Code Playgroud)

用法

./progress_adb.sh source destination
Run Code Online (Sandbox Code Playgroud)

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 
Run Code Online (Sandbox Code Playgroud)

在Ubuntu 14.04上进行测试

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

去做

目录支持

屏幕尺寸更改时,进度条大小更改


joe*_*cks 4

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

ADB_TRACE=adb adb push <source> <destination> 
Run Code Online (Sandbox Code Playgroud)

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

writex: fd=3 len=65544: 4441544100000100000000021efd  DATA....#....b..
Run Code Online (Sandbox Code Playgroud)

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

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