-1

I use output from some commands to get path of .apk file and then pull that file from Android phone like this:

#!/bin/bash
#This command sets APK_PATH variable to /data/app/com.test.something-1/base.apk
APK_PATH="$(adb shell pm path com.test.something | cut -d':' -f 2)"
# adb pull command is being used to pull .apk file from phone when I have the path
adb pull $APK_PATH

This dynamic approach to getting .apk path and pulling file from phone doesn't work. It results with following error message:

' does not existdata/app/com.test.something-1/base.apk

However, when I hardcode path, it works perfectly:

#!/bin/bash
APK_PATH=/data/app/com.test.something-1/base.apk
adb pull $APK_PATH

Can anyone help me with this little piece of bash scripting? I believe that somehow APK_PATH is not yet set when 'adb pull' starts executing but can't fix it.

Drag0
  • 8,438
  • 9
  • 40
  • 52
  • I love "dynamically set variable" :-) If it was statically set, wouldn't it be a constant? ;-) – SaintHax Apr 21 '16 at 12:55
  • It's pretty obvious that `adb shell pm path com.test.something | cut -d':' -f 2` does not return the correct path. echo out APK_PATH after setting it and see what is there. – 123 Apr 21 '16 at 12:58
  • When I print it out, it is correct. Please read description. – Drag0 Apr 21 '16 at 13:01
  • @Drag0 Where does it say that in the description? It is obviously not correct or it would work. – 123 Apr 21 '16 at 13:04
  • @123 - oh, sorry I didn't write it in description, but I echoed it out and it was correct path. Maybe there were some hidden chars that Terminal wouldn't show. – Drag0 Apr 21 '16 at 13:06

1 Answers1

1

I'm guessing you are getting an unwanted special character in the returned value, b/c of the weird missing space and leading / (unless you just typed in your info wrong).

Try this

APK_PATH="$(adb shell pm path com.test.something |tr -cd '[:graph:] \t' |cut -d':' -f2)"

SaintHax
  • 1,875
  • 11
  • 16
  • Hmm, it's funny. Your code returns same path, at least that I can see how it prints out on screen. But when I use your way of getting string, it works. Can you explain more what you did? – Drag0 Apr 21 '16 at 13:04
  • Sure, the tr (translate) removes all non-printable characters and white spaces (which, in hindsight, could be in your path, so I'll edit). Your command was returning two backspaces, it seems. That was eating your leading slash, so the tr removes them before storing the string into your variable. – SaintHax Apr 21 '16 at 13:23