Search code examples
androidbashshellapkadb

passing adb output to variable in bash script


im trying to create bash script to install split APKs manually with adb shell that requires to get session id using the command bellow

command

SESSION='pm install-create -S 42211368'

this will output something like : Success: created install session [547376362]

547376362 will be the session ID

I want to pass 547376362 into SESSION Variable

sh < pm install-write -S 24628703 ${SESSION} 0 /sdcard/YTAPKM/base.apk

so result shall be "sh < pm install-write -S 24628703 547376362 0 /sdcard/YTAPKM/base.apk"


Solution

  • grep is sufficient for this.

    SESSION=$(pm install-create -S 42211368 | grep -oE '[0-9]+')
    sh < pm install-write -S 24628703 ${SESSION} 0 /sdcard/YTAPKM/base.apk
    

    To explain what's happening a bit:

    • grep -E uses "extended" regular expressions (easier to work with)
    • grep -o outputs only the matching part, the integer in this case
    • SESSION=$(some_cmd) stores the stdout from some_cmd to the variable SESSION, and allows for pipes and such too