0

I am building a bash script for recursively copy files from the Android data folder using adb shell command. The code is like

touch2() { mkdir -p "$(dirname "$1")" && touch "$1" ; }

PHONE_ADDR="192.168.0.105"
PHONE_PORT="5555"

adb connect ${PHONE_ADDR}:${PHONE_PORT}
adb shell "run-as com.company.package find ./databases -type f" > temp.txt

input="temp.txt"
while IFS= read -r line
do
  echo $line
  touch2 $line
  adb shell "run-as com.company.package cat $line" > $line
#   BACK_PID=$!
#   wait $BACK_PID
done < $input

adb disconnect ${PHONE_ADDR}:${PHONE_PORT}

Basically I first list all files' path in Android data folder and storage them in temp.txt. Then I would like to read trough temp.txt line by line and process each file with an adb shell command.

But it only processes the first line of file in temp.txt. I am thinking of if the while loop is not waiting for the adb shell command to finish. So I have tried to add a wait command but it does not work.

IvonChan
  • 9
  • 2
  • Does `adb shell` read from standard input? If so, it's probably stealing the rest of the file list. See [BashFAQ #89](http://mywiki.wooledge.org/BashFAQ/089) and [this question](https://stackoverflow.com/questions/62585875/bash-script-do-loop-exiting-early). – Gordon Davisson Sep 25 '22 at 04:26
  • Thank you very much Gordon! You pointed out the problem. Using a different file descriptors is working. – IvonChan Sep 25 '22 at 07:11
  • Great! I'm going to mark this as a duplicate of one about the same basic problem (but a different specific command causing it) -- it's better for site organization purposes to funnel similar questions into a single answer (/collection of answers). – Gordon Davisson Sep 25 '22 at 08:24

0 Answers0