Bash脚本:把安卓手机上的照片或截屏以增量的方式下载到本地

Written by 汶水一方 on 2019.07.08 14:41:22 字数 88 阅读 618

写了一个Bash脚本,把安卓手机上的照片或截屏以增量的方式下载到本地,即每次运行,只下载本地没有的文件,如果本地已经有同名文件,则会比较MD5值,不同时下载,相同时跳过。在MacOS上测试通过。


#!/bin/bash
counter=0

PS3='Choose where to download photos from the connected Android phone: '
options=("Camera" "Screenshot" "Do nothing" "Quit")
select opt in "${options[@]}"
do
    case $REPLY in
        "1")
            echo 
        TARGET_DIR="/sdcard/DCIM/Camera/"
        break
            ;;
        "2")
            echo
        TARGET_DIR="/sdcard/Pictures/Screenshots/"
            break
            ;;
        "3")
            echo "You chose choice $REPLY which is $opt"
            ;;
        "4")
            exit 1
            ;;
        *) echo "Invalid option $REPLY";;
    esac
done

FILE_LIST=`adb shell find $TARGET_DIR -maxdepth 1 -type f`

for file_with_path in $FILE_LIST
do
    NAME_with_EXT=${file_with_path##*/}
    if [ ! -f "$NAME_with_EXT" ]
    then
        echo "> "$NAME_with_EXT
        echo -ne "  + Pulling file to local: $NAME_with_EXT"
        adb pull $file_with_path
        counter=$((counter+1));
    else
        echo "> "$NAME_with_EXT
        echo -ne "  ? Already exists. Comparing MD5 now ... "
        MD5phone=`adb shell md5sum $file_with_path |cut -d' ' -f 1`
        MD5local=`md5 $NAME_with_EXT |cut -d' ' -f 4`

        if [ "$MD5phone" == "$MD5local" ]; then
            echo "Same MD5. Ignore"
            echo $filename
        else
            echo "Different MD5. Rename the local file, and pull the remote file to local..."
            nameonly="${NAME_with_EXT%.*}"
            extension="${NAME_with_EXT##*.}"
            newname=$nameonly-$RANDOM.$extension
            while [ -f $newname ]
            do
                newname=$nameonly-$RANDOM.$extension
            done
            mv $NAME_with_EXT $newname
            echo -ne "      "
            adb pull $file_with_path
            counter=$((counter+1));
        fi
    fi
done

echo
echo Total files downloaded: $counter