8

I'm coding a script to automate an application configuration export process.

adb shell
...
<open app menu>
<open app settings>
<save app backup>
...
exit

The problem is: after saving the backup I want to pull the file to my computer, using this:

adb pull /storage/emulated/0/Backups/ ~/Documents/phone

Although, I can't figure out how to execute this command, after exiting adb shell. It seems like commands after exit are ignored:

adb shell
...
<open app menu>
<open app settings>
<save app backup>
...
exit
adb pull /storage/emulated/0/Backups/ ~/Documents/phone

Maybe, this is an issue I should addres to the "Unix & Linux" community, but I decided to try here first.

2
  • 1
    Instead of writing the backup to file(s) write them to stdout and execute the command using adb exec-out ... > localfile. Then the file will be executed on the phone and the output will be written to your PC disk. – Robert Dec 14 '20 at 17:14
  • works fine to me.. try a second terminal – alecxs Dec 14 '20 at 17:57
12

Write your script a little different:

#!/bin/bash
adb shell <<EOF
...
<open app menu>
<open app settings>
<save app backup>
...

EOF

adb pull /storage/emulated/0/Backups/ ~/Documents/phone

All between <<EOF and the EOF standing separately at the start of its own line is run in adb shell, all outside is run on your local machine. Note: no exit needed.

2
  • 4
    The syntax used is an "heredoc". I strongly recommend reading on stackoverflow.com/questions/2953081/…. O.P. didn't post the real contents of the script, which may or may not contain variable names supposed to be used in the adb shell, or outside, and that question there contains information about the syntax and other things to note. – Ismael Miguel Dec 15 '20 at 1:39
  • 1
    Good point, yes – I didn't get on the name. However, I use HEREDOC a lot, and just verified it again now: other than the top answer there suggests, variables are interpreted inside HEREDOC (tried the given example, like foo=FOOBAR\ncat <<EOF\necho this is ${foo}\nEOF, and it printed "This is FOOBAR"). And yes, as in the example, I use Bash; worked with cat as well as with adb shell. Never blindly believe without verification, even answers upvoted like that one ;) What however does NOT work: setting the var inside the HEREDOC will have no effect of "afterwards". – Izzy Dec 15 '20 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.