adb在bash中不使用变量

时间:2015-04-03 11:09:04

标签: android linux bash adb

我试图从我的Android手机中导出所有apks(rooted)。

我已将使用adb shell pm list的所有软件包转储到文件中。

com.google.android.ears
com.android.voicedialer
...

现在我想下载它们,但它不起作用:

#!/bin/bash

while read line; do
        echo "Name: $line"
        # get package path
        p=$(adb shell pm path $line);
        # remove package: prefix
        p=$(echo $p | grep -Po "(?<=package:).*" | xargs)
        echo "Path: $p"
        # download it
        adb pull $p
done;

我得到这样的东西:

Name: com.google.android.ears
Path: /system/app/GoogleEars.apk
' does not exist/system/app/GoogleEars.apk
Name: com.android.voicedialer
Path: /system/priv-app/VoiceDialer.apk
' does not exist/system/priv-app/VoiceDialer.apk
Name: com.android.defcontainer
^C

当我在终端手动完成时,它可以正常工作。

为什么它不能在这里工作?

1 个答案:

答案 0 :(得分:0)

正如评论中所指出的,adb命令以\r结尾。您也必须将其从此行中删除

p=$(adb shell pm path $line)

类似

p=$(adb shell pm path $line | tr -d '\r')

我看到另一个问题:许多apks将被覆盖为base.apk

Name: com.google.android.youtube
Path: /data/app/com.google.android.youtube-1/base.apk
Name: net.mx17.overridedns
Path: /data/app/net.mx17.overridedns-2/base.apk
Name: com.android.email
Path: /system/app/Email/Email.apk

所以我想我会用

之类的东西来做
#!/bin/bash

while read line; do
  echo "Name: $line"
  # get package path
  p=$(adb shell pm path $line | sed 's/^package://;s/\r$//')
  echo "Path: $p"
  # get package name
  name=$(echo $p | awk -F/ '{print $(NF-1)}')
  echo "Package name: $name"
  # download it
  adb pull $p ${name}.apk
done < <(adb shell pm list packages | sed 's/^package://;s/\r$//')