shell script to get file from android sdcard with adb shell [duplicate] - android

This question already has answers here:
Copy a batch of files from Android to PC
(2 answers)
Closed 4 years ago.
#!/bin/bash
DIR_PATH="/sdcard/log/xxxxdir"
function useAdbReadLastFileFromDir(){
fileName=`adb shell ls $1|tail -1`
if [ -n $fileName ]
then
echo "fileName:"
echo "adb pull $PATH/$fileName"
fileContent=`adb pull $PATH/$fileName`
echo "fileContent:"$fileContent
else
echo "file not found exception"
fi
}
useAdbReadLastFileFromDir $DIR_PATH
android6.0 yotaphone/sunsang note5 can't pull file from sdcard, error message said:
file or dir not exist
but androi8.0 oppo findx/mi mix2 does.
help~!

adb pull takes 2 mandatory parameters: REMOTE_FILENAME and LOCAL_FILENAME
If you want to adb pull a text file to stdout - use adb shell cat FILENAME instead

Related

Loop in a bash script with adb command [duplicate]

This question already has answers here:
Loop to find an app from a file and pull it with adb shell
(2 answers)
Closed 4 years ago.
I'm beginning with bash ans I'm trying to loop in a shell script to pull different apk's from my android device.
I've got a file with apk's paths
apkpaths.txt
/data/app/com.naver.linewebtoon-1/base.apk
/data/app/com.game5mobile.lineandwater-1/base.apk
I read this file and try to applicate this script but only the first apk is pulled but three times. How can I create a single file app.apk for each apk I pull with the adb command line ?
#!/bin/bash
filename="$1"
while IFS=: true; do
line=''
read -r line
if [ -z "$line" ]; then
break
fi
END=3
for i in $(seq 1 $END);
do adb shell cat $line > app$i.apk;
done
#echo "$line"
done < "$filename"
Thank you
#!/bin/bash
fileNum=1
while IFS='' read -r line || [[ -n "$line" ]]; do
adb shell cat $line > app$fileNum.apk
let "fileNum++"
done < "$1"

adb REMOTE pull file to desktop [duplicate]

This question already has answers here:
Android: adb pull file on desktop
(6 answers)
Closed 4 years ago.
Android: adb pull file on desktop
Trying to copy file from remote device to desktop, here is a command:
adb -s xx.xx.xx.xx:5555 pull sdcard/screen.png c:/users/xx/desktop/
But I always get the message:
cannot create 'c:\users\xx\desktop\': No such file or directory!! | in sdcard
if i run the same command whitout dir path
adb pull <remote> [local]
adb -s xx.xx.xx.xx:5555 pull sdcard/log.txt log1.txt
it well copy file screen.png to the same android sdcard dir not sdk folder in my windows Os
do adb has the ability to transfer file over tcp/ip like the documentation (adb pull <remote> [local]) or im wrong ??
If I am not wrong, "/" is for defining path in Unix systems. Try to use "\" as Windows systems define path.

Busybox ash - execution with permission denied [duplicate]

This question already has an answer here:
Android: Executing a program on adb shell
(1 answer)
Closed 4 years ago.
I'm trying to run busybox on an Android device, I pushed busybox binary to /tmp folder, then add a i.sh file in /tmp folder with code below:
#!/bin/sh
echo aa
/tmp/busybox dirname
Then enter /tmp folder and execute like this: ./busybox ash ./i.sh
Error report:
./i.sh: line 3: /tmp/busybox: Permission denied
Device is rooted, setenforce has set to 0.
If use system sh to execute this file sh ./i.sh, everything is ok.
If you don't have root access in your Android you can copy busybox into the path "/data/local/tmp" and run it like "/data/local/tmp/./busybox"

adb shell one-liner not passing output properly [duplicate]

This question already has an answer here:
Send data back to the script which started the activity via adb shell am start
(1 answer)
Closed 4 years ago.
I am trying to write a script that will pull any photos or videos I've taken today. Here is what the code looks like:
for i in $(adb shell ls -l /sdcard/DCIM/Camera/ | grep $(date +%Y-%m-%d) | awk '{ print $7 }' ) ; do adb pull /sdcard/DCIM/Camera/$i ~/Photos ; done
And here is the error I get when I run it:
' does not existsdcard/DCIM/Camera/IMG_20160507_012827.jpg
It properly grabs the name the file(s) that need to be pulled, but for some reason it doesn't pass that information to "adb pull" properly.
Do I need to do something else to "sanitize" the output of one command into the input of the other?
why don't you just try find to list down all the files which are modified in last 24 hours and copy them to desired directory ?
find ~/desired/source -mtime -1 -type f -print0 | xargs -0 cp -t ~/destination/picture

strange behaviour of adb pull in bash script [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I want to write a bash script to fetch all WhatsApp's backups automatically, but I cannot understand what's wrong
#!/bin/bash
adb start-server
for file in $(adb shell ls /sdcard/WhatsApp/Databases)
do
adb pull /sdcard/WhatsApp/Databases/$file
done
The output is very strange:
$ ./script.sh
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7
' does not existsdcard/WhatsApp/Databases/msgstore.db.crypt7
since "adb shell ls /sdcard/WhatsApp/Databases" works, there shall be no problem pulling each file into the current working directory, but as you can see, even the error message doesn't look well formed ("' does not existsdcard/WhatsApp...")
I tried echo "adb pull /sdcard/WhatsApp/Databases/$file" instead of directly calling adb, and if I copy/paste each single line of the output into the shell, it works.
But any other attempt to do this automatically fails.
I feel like if I forgot some very basic concepts of bash scripting
Edit:
After running it with
#!/bin/bash -x
(thanks to Rakesh Gupta suggestion) the output is the following:
+ adb start-server
++ adb shell ls /sdcard/WhatsApp/Databases
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-28.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-29.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-10-30.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-01.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-02.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-03.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore-2014-11-04.1.db.crypt7' does not exist
+ for file in '$(adb shell ls /sdcard/WhatsApp/Databases)'
+ adb pull $'/sdcard/WhatsApp/Databases/msgstore.db.crypt7\r' .
remote object '/sdcard/WhatsApp/Databases/msgstore.db.crypt7' does not exist
and indeed there is the \r character at the end of each file name that is likely to cause that behaviour.
Thanks to that other guy that linked me to https://stackoverflow.com/tags/bash/info, and adding "tr -d '\r'", in this way the script runs nicely
#!/bin/bash
adb start-server
for file in $(adb shell ls /sdcard/WhatsApp/Databases | tr -d '\r')
do
adb pull /sdcard/WhatsApp/Databases/$file .
done
There may be a special character in you script. Try running dos2unix on your script. Also, try adding -n as under to check the syntax
#!/bin/bash -n
and run the script to identify any issues with syntax.
You can also try
#!/bin/bash -x
to enable the debug mode

Categories

Resources