0

Using a bash script to automatically pull and install an android app via ADB. However, I get an error ' does not existte object [apk path]`. Here is the script:

#! /bin/bash

APK_PATH="$(adb shell pm path $1)"
echo "${APK_PATH#*:}"
APK_PATH=${APK_PATH#*:}
adb pull $APK_PATH
mv base.apk $1.apk


if [ "$2" == "--jadx" ] || [ "$2" == "-j" ]
    then jadx $1
fi

How do I solve this.

NOTE: I used an alias to the script location so I just need to run autoapk.

For the specific error, I ran autoapk b3nac.injuredandroid and got the error message

/data/app/b3nac.injuredandroid-1/base.apk
' does not existte object '/data/app/b3nac.injuredandroid-1/base.apk
mv: cannot stat 'base.apk': No such file or directory
Ferdinand
  • 513
  • 1
  • 3
  • 9
  • Does your script file have Windows style CRLF line endings when you're in a Linux/Unix/etc. system that expects LF endings? – Shawn Mar 01 '22 at 21:22
  • Clearly the `base.apk` file doesn't exist, or isn't where the program expects it to be. What is supposed to create it and where is it supposed to put it. – pjh Mar 01 '22 at 22:35
  • Use tracing to see exactly what the code is doing. Run with `bash -x` or put `set -o xtrace` (or `set -x`) at the start of the program. See [How can I debug a Bash script?](https://stackoverflow.com/q/951336/4154375). – pjh Mar 01 '22 at 22:37
  • [Shellcheck](https://www.shellcheck.net/) identifies a bunch of missing quotes in the code. It's almost always a good idea to fix issues identified by [Shellcheck](https://www.shellcheck.net/). – pjh Mar 01 '22 at 22:40
  • @pjh I am using Genymotion and this is an app installed on the emulator. The adb shell pm path commands lists the path to the apk and this is /data/app/b3nac.injuredandroid-1/base.apk. It is the same when I cd into the directory – Ferdinand Mar 02 '22 at 17:46
  • @pjh when I run bash -x I get ```++ adb shell pm path b3nac.injuredandroid + APK_PATH=$'package:/data/app/b3nac.injuredandroid-1/base.apk\r' + echo $'/data/app/b3nac.injuredandroid-1/base.apk\r' /data/app/b3nac.injuredandroid-1/base.apk + APK_PATH=$'/data/app/b3nac.injuredandroid-1/base.apk\r' + adb pull $'/data/app/b3nac.injuredandroid-1/base.apk\r' ' does not existte object '/data/app/b3nac.injuredandroid-1/base.apk + mv base.apk b3nac.injuredandroid.apk mv: cannot stat 'base.apk': No such file or directory + '[' '' == --jadx ']' + '[' '' == -j ']' ``` – Ferdinand Mar 02 '22 at 17:49
  • The `\r` in the `bash -x` output is characteristic of a CRLF line termination issue. See [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/q/39527571/4154375) and [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/4154375). – pjh Mar 02 '22 at 20:20

1 Answers1

2

Start by checking the file is where it is expected as

...
adb pull $(tr -d '\r' <<< "$APK_PATH") "$1.apk"
if [[ -r  "$1.apk" ]]
then
    printf 'OK\n'
else
    printf 'ERROR: %s not found\n' "$base"
    exit 1
fi
...

Do the same for all of your other expectations.

edit

I guess this is what you are expecting, to rename the apk.

edit 2

It seems you have a '\r`

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Just by pasting in your code I get ```' does not existte object '/data/app/b3nac.injuredandroid-1/base.apk ERROR: base.apk not found``` This suggests base.apk does not exist. This is unusual given I can literally cd into the directory /data/app/b3nac.injuredandroid-1 and there I type in the command ls and I find base.apk – Ferdinand Mar 02 '22 at 18:01
  • This works! Is the reason because my file has "Windows style CRLF line endings when you're in a Linux/Unix/etc. system that expects LF endings?" as @pjh suggested? – Ferdinand Mar 02 '22 at 19:27
  • Not your file, the name of your file contains the `\r` – Diego Torres Milano Mar 02 '22 at 19:33
  • b3nac.injuredandroid or autoapk? – Ferdinand Mar 02 '22 at 19:39
  • the output of `adb shell`. Run `adb shell pm path yourpkg | xxd` or something similar to verify. – Diego Torres Milano Mar 02 '22 at 19:51
  • 1
    It's possible that `adb shell pm path yourpkg` is producing CRLF terminated output. Another possibility is that the `APK_PATH="$(adb shell pm path $1)"` line in the program has a CRLF line ending. It is possible (indeed, common) for files to have mixed LF and CRLF line endings. A good code editor will highlight such problems. – pjh Mar 02 '22 at 20:18