Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xxnjdlys/624e20f417fd1c7819c27187d54f6733 to your computer and use it in GitHub Desktop.
Save xxnjdlys/624e20f417fd1c7819c27187d54f6733 to your computer and use it in GitHub Desktop.
#!/bin/bash
get_adb_devices()
{
adb $ADB_SERVER devices $LONG 2>&1 | tail -n +2 | sed '/^$/d'
}
git_pull()
{
[[ "$UNAME" == 'Linux' ]] && OPT=-e
ASD=$(readlink $OPT $0)
if [[ -n "$ASD" ]]
then
DIR=$(dirname $ASD)
if [[ -n "$DIR" && -d "$DIR/.git" ]]
then
(cd $DIR && git pull)
fi
fi
}
PROGNAME=$(basename $0)
VERSION="3.2.0"
UNAME=$(uname)
DEVICE_OPT=
LONG=
ADB_SERVER=
git_pull > /dev/null
for opt in "$@"
do
case "$opt" in
-d|-e|-s)
DEVICE_OPT=$opt
;;
-l|--long)
LONG=-l
;;
start-server|kill-server|connect|-help)
exit 0
;;
-V|--version)
echo "$PROGNAME version $VERSION"
exit 0
;;
esac
done
[ -n "$DEVICE_OPT" ] && exit 0
DEV=$(get_adb_devices)
if [ -z "$DEV" ]
then
echo "$PROGNAME: ERROR: There's no locally connected devices." >&2
read -p "Do you want to connect to a remote adb server? [Y/n]: " REPLY
case "$REPLY" in
n|N)
exit 1
;;
y|Y|"")
read -p "ADB server IP: " IP
ADB_SERVER="-H $IP"
DEV=$(get_adb_devices)
;;
esac
elif echo "$DEV" | grep -q 'daemon started successfully'
then
# try again
DEV=$(get_adb_devices)
fi
N=$(echo "$DEV" | wc -l | sed 's/ //g')
case $N in
1)
# only one device detected
D=$DEV
;;
*)
# more than one device detected
OLDIFS=$IFS
IFS="
"
PS3="Select the device to use, <Q> to quit: "
select D in $DEV
do
[ "$REPLY" = 'q' -o "$REPLY" = 'Q' ] && exit 2
[ -n "$D" ] && break
done < /dev/tty
IFS=$OLDIFS
;;
esac
if [ -z "$D" ]
then
echo "$PROGNAME: ERROR: target device couldn't be determined" >&2
exit 1
fi
# this didn't work on Darwin
# echo "-s ${D%% *}"
SELECTED_DEVICE=$(echo ${D} | sed 's/ .*$//')
# echo ${SELECTED_DEVICE}
# adb -s ${SELECTED_DEVICE} shell
SS_DIR=$HOME/share/screenshot
if [ -d "$SS_DIR" ];
then
echo "screenshot save file dir exist "
else
mkdir -p ${SS_DIR}
fi
DEVICE_SS_DIR=/data/local/tmp/
cd ${SS_DIR}
#DATE=$(date +%m%-d%-H%-M%-S)
DATE=$(date +%s)
#if perl < /dev/null > /dev/null 2>&1 ; then
# 电脑上有 perl 执行下面的命令
#adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > ${DATE}.png
#else
# 电脑上没 perl,多次使用 adb 命令
adb -s ${SELECTED_DEVICE} shell screencap -p ${DEVICE_SS_DIR}${DATE}.png
adb -s ${SELECTED_DEVICE} pull ${DEVICE_SS_DIR}${DATE}.png
#fi
# 打开文件夹
# mac 上使用open 命令,
# linux使用 naultilus 命令,
# windows 使用 explore 命令
open $SS_DIR
# echo "Please check your screencap picture in "${SS_DIR}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment