1

I'm trying to pull Whatsup images from my android phone via ADB using the following script:

HOST_DIR=/myImageDirectory
DEVICE_DIR="/sdcard/WhatsApp/Media/WhatsApp\ Images"
echo started
for file in $(./adb shell ls $DEVICE_DIR)
do
    file=$(echo -e $file | tr -d "\r\n"); # EOL fix
echo $file  
    ./adb pull $DEVICE_DIR/$file $HOST_DIR/;
done

The space in the source directory is escaped successfully for ls but not for pull. That is, i get the list of files but cannot pull them. Here are 3 lines of the output:

IMG-20171101-WA0003.jpg
adb: error: remote object '/sdcard/WhatsApp/Media/WhatsApp\' does not exist
adb: error: remote object 'Images/IMG-20171101-WA0003.jpg' does not exist

What gives?

1

Get doublequotes:

for file in $(./adb shell ls \""$DEVICE_DIR"\")
                               ^           ^
./adb pull "$DEVICE_DIR/$file" $HOST_DIR/;
           ^                 ^

And remove backslash escapes:

DEVICE_DIR="/sdcard/WhatsApp/Media/WhatsApp Images"
                                           ^
  • Thanks, that did the trick! I now get a different error: adb: error: remote object '/... /WhatsApp\ Images/IMG-20171031-WA0000.jpg' does not exist. But i can pull one file at the time, it clearly does exist. – Ivana Nov 6 '17 at 4:34
  • @Ivana Updated. Remove backslash escapes as it's counter-productive in quoted strings. – iBug Nov 6 '17 at 4:53
  • @iBug I usually set `IFS=$'\n' before playing with spaces, as to avoid headaches. You may want to add this in your answer. – Death Mask Salesman Nov 6 '17 at 15:34
  • @DeathMaskSalesman But I myself ain't very familiar with IFS... – iBug Nov 6 '17 at 16:03
  • @iBug As I trust you're aware, IFS specifies what character the shell interprets as separator. The default character is a blank, which makes the shell choppy when having blanks within paths. IFS='\n' tells the shell to treat a newline as the separator, so that one can safely meddle with the abovementioned kind of paths. – Death Mask Salesman Nov 6 '17 at 16:08
0

The answer by iBug solves the first problem but creates another. The trick is to leave out the quotes around the directory name, and only add them around the directory variable, like this:

HOST_DIR=/myImageDirectory
DEVICE_DIR="/sdcard/WhatsApp/Media/WhatsApp\ Images"
DEVICE_DIR_noescape=/sdcard/WhatsApp/Media/WhatsApp\ Images
echo started
for file in $(./adb shell ls $DEVICE_DIR)
do
    file=$(echo -e $file | tr -d "\r\n"); # EOL fix
    echo $file  
    ./adb pull "$DEVICE_DIR_noescape/$file" $HOST_DIR/;
done

And i yes need to clean up the code, but this works.

  • iBug has already updated his answer – yours now causes some confusion, especially as you're mixing quoting and escaping. As you basically want to say the same: wouldn't you agree iBug's answer suffices? – Izzy Nov 6 '17 at 7:09
0

for ADB (not shell) command before and after SPACE input " example adb pull /sdcard/Wi-Fi" "Direct /C:/1/

result files from "Wi-fi Direct" folder copied to folder 1 in C hard drive

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.