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

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

Related

Fetching most recent file and deleting it via adb

I'm trying to do a pipe operation on my Android device through adb (this is for an automated script).
The operation is to fetch the most recently modified file in a particular directory and then delete it.
Let us say this file is file.txt and it is in /sdcard/Android/data/my.app.package on the Android device.
When I try to do adb shell ls -t /sdcard/Android/data/my.app.package | head -1 | xargs rm -f it throws the error:
rm: file.txt: No such file or directory
This is because it expects the full path.
So then I tried ls -t /sdcard/Android/data/my.app.package | head -1 | xargs ls -d | xargs rm -f but it complains with the same error.
Perhaps I need to pass in the $PWD along with the file name to xargs. How can I do that, or is there a better way to do this?
Edit: I have now tried ls -t sdcard/Android/data/my.app.package | head -1 | xargs -I '{}' ls sdcard/Android/data/my.app.package/'{}' and while a similar command works correctly on the Linux system as expected, it does weird stuff on the Android device. Possibly some missing implementation of xargs on Android stack.
A command like
adb shell foo | bar
runs foo in adb shell, and has your local shell run bar and receive its input from adb shell. If bar should run in adb shell too, you want to pass the entire pipeline to adb shell:
adb shell 'foo | bar'
This part of your question is basically a duplicate of
How to type adb shell commands in one line?
Separately, your ls -t command is flawed in several ways. Generally, don't use ls in scripts; but the trivial fix is to run the command in that directory directly. Then you don't need to add the path back on:
adb shell 'cd /sdcard/Android/data/my.app.package &&
ls -t | head -1 | xargs rm -f'
This still suffers from the various vagaries of parsing ls output; probably a better solution is to use find instead. If you have the facilities of GNU find and related utilities available on the remote device, try
adb shell 'find /sdcard/Android/data/my.app.package -printf "%T# %p\\0" |
sort -r -z -n | head -z -n 1 | sed "s/^[^ ]* //" | xargs -0 rm'
(Adapted from https://stackoverflow.com/a/299911/874188 with quoting fixes to allow the overall command to be run inside single quotes.)
If adb shell does not provide access to GNU userspace tools, perhaps just make sure you have very detailed control over what files can land in your directory so that you can reasonably reliably parse the output from ls; or if you can't guarantee that, try hacking something in e.g. Perl.
It is unfortunate that there is no simple standard way to get the oldest or newest n files from a directory in a robust, machine-readable form.
It would be nice if there was a more succinct way than these arguably complex and slightly advanced tricks with find.
This is the solution that works:
adb shell ls /sdcard/Android/data/my.app.package/`adb shell ls -t /sdcard/Android/data/my.app.package | head -1` | adb shell xargs rm -f

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 logcat exit on tag [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.
Im trying to filter android logcat at real time and execute some command or exit from logcat when specific tag occured. I tried the following:
adb shell "logcat | grep 'sometag' && echo 'Tag occured'"
adb shell logcat -m --regex='sometag'
In second command -m does not work at all, but is listed in logcat documentation.
Any ideas how can i do this?
Answer (maybe temporary because infinite loop is not a best idea):
Because i'm scripting in PowerShell i found that solution:
While(1) {
$log = & $abdPath shell "logcat -d | grep 'onPause'"
if($log) {
break;
}
}
Thanks for down voting without any reason :)

How to pull photos with ADB from local storage - android device

i have some problems with my Asus Zenfone 2 device (no root).
In particular, some days ago, while i was travelling abroad, my device decided to no more turn on, staying permanently in the loading ASUS screen.
Before doing the hard reset, i want to try to save at least all my photos located in the internal storage (N.B: i do not have an external SDCARD).
i correctly set up the ADB software to recognize my device but i am not able to find the photo directory.
Till now i have only pull successfully 2 directory: "sys" and "cache", using this command:
adb pull / C:\Myfile
It seems to transfer only some file system.
Doas anyone know how to pull photos or other file?
Use adb pull sdcard/DCIM/Camera C:\MyFile
If it didn't work use:
adb shell
echo $EXTERNAL_STORAGE
It will print path to your simulated external storage. Then:
cd $EXTERNAL_STORAGE/DCIM
ls
It will print folder which will contain camera app folders. Lastly, use:
adb pull HERE_PUT_PATH_TO_EXTERNAL/DCIM/SELECTED_CAMERA_APP_FOLDER C:\MyFiles
Note: To leave adb shell type exit
I did 3 things to pull all files of the same type, in this case *.mp4 (all videos)
1. First copy the output of: {all videos listed} with xargs to a file.txt
adb shell ls /storage/7C17-B4FD/DCIM/Camera/*.mp4 | xargs -t -I % echo % >> file.txt
adb shell ls /storage/7C17-B4FD/DCIM/Camera/*.mp4 returns all videos listed in device.
The argument -t show the output from XARGS, and -I lets you use a variable, in this case represented with % then you write the command you want to do. In this case, echo % each line and add it to the file.txt
2. You need to clean the file.txt from spaces and the '\r' you do it with the command tr
tr -d '\r' < file.txt > text-clean.txt
the input file is < file.txt and the new output file is > text-clean.txt
3.Now you can cat the new clean file text-clean.txt and use that input with XARGS command to pass the command adb pull for each file.
cat text-clean.txt | xargs -t -I % adb pull % ./
we cat the content of text-clean.txt and send the output to XARGS, then we see what is the result of command with -t and -I to add variables. adb pull to request file from path which is represented with % and ./ means copy to this directory or currrent directory of where you are.
if you just need to copy one file. just need to find the file with adb shell ls /path/to/file and the just copy to your location with adb pull. example
adb pull /storage/7C717-B4FD/DCIM/Camera/video1.mp4 ./

How can I create the following Android bash script?

When I type:
adb devices
My output is (this can be variable, it can list 10 or 20 etc):
List of devices attached
0280414640c133d7 device
TA054085R1 device
Afterwards I'd like to run:
adb install MyApp 0280414640c133d7
adb install MyApp TA054085R1
How can I get this going in a bash script?
I'm not sure how robust you need your solution to be, but something like this will work with the case you describe above:
#!/bin/bash
echo "Deploying SONR to devices..."
#install SONR
for foo in `adb devices | egrep 'device$' | cut -d ' ' -f1`
do
adb -s $foo install SONR.apk
done
It is no doubt possible to replace the ugly egrep piped through cut with a single call to sed or awk or even a perl one-liner.

Categories

Resources