Skip to content Skip to sidebar Skip to footer

Get Result Code Of Adb Shell Command

How should I get $? of adb shell ? I would like to check the result of adb shell mkdir /xxx. I executed mkdir command by adb shell and failed but the result of $? is

Solution 1:

You should do:

$ adb shell 'mkdir /xxx; echo $?'mkdir failed for /xxx, Read-only file system
255

Notice the single quotes, otherwise $? is evaluated before reaching adb.

Solution 2:

If you are using an older emulator, then adb shell does not return the exit value. Then you have to use adb shell 'false; echo $?' like in https://stackoverflow.com/a/17480194/306864

In newer emulators, it works properly, then you can do adb shell false || echo failed. Here's how I tested:

$ adb -e android-22 shell false; echo $?
0
$ adb -e android-22 shell 'false; echo $?'
1
$ adb -e android-29 shell false; echo $?
1
$ adb -e android-29 shell 'false; echo $?'
1

Solution 3:

You should check the return value of adb shell itself instead of checking the last return value on the device. For example, execute adb shell ls and then execute echo $? (from your shell, not from within the device, i.e. don't execute adb shell echo $?) and you'll see a return value of 0. If you execute adb shell dlfkgjdlkf and then echo $? you'll see a return value of 1.

Post a Comment for "Get Result Code Of Adb Shell Command"