I have my laptop connected to an android phone.
I am doing a task many times, so I wish to write a .bat file to run the commands automatically.
adb shell
cd /sdcard/speech
rm -f *
The bat file only executed adb shell, the rest codes were not executed.
I guess because the it entered the android device so the commands did not run as usual.
One solution was adb shell rm -f -r /sdcard/speech/*
What if there are more and more complicated commands?
Is there a way to do it ?
You can do your job with adb shell "cd /sdcard/speech; rm -f *".
For more complicated jobs, you can put all the commands in a Linux shell script, use adb push command to push the script to your Android device, and run the script using adb shell.
For example, put all the commands in run.sh, then issue:
adb push run.sh /data/local/tmp
adb shell "chmod +x /data/local/tmp/run.sh"
After this you can run your jobs with:
adb shell "/data/local/tmp/run.sh"
You can include the above line in a .bat file.
Related
Below command is to pull a file:
adb -d shell "run-as com.myapp cat /data/data/com.myapp/databases/file.db" > file.db
But how to push it back like Android Studio does via Device File Explorer?
There is no simple command for uploading the file. What Android Studio does when uploading a file using Device File Explorer is this:
Upload the file via adb push to /data/local/tmp/<random file name>
Execute adb shell run-as com.myapp sh -c 'cp /data/local/tmp/<random file name> /data/data/com.myapp/<path>/<final file-name>'
Delete the temp file via adb shell rm /data/local/tmp/<random file name>
Get the updated view for Device File Explorer using adb shell run-as com.myapp sh -c 'ls -al /data/data/com.myapp/<path>/'
I discovered this by capturing the adb traffic on TCP port 5027 using Wireshark. An interesting detail is that each command executed using adb shell command uses the form <command-to-be executed in adb shell> || echo ERR-ERR-ERR-ERR
From Robert's answer now I can do like this:
function dbpull() {
adb shell run-as "com.$1.debug" cat "/data/data/com.$1.debug/databases/$2.db" > "/Users/username/Desktop/$2.db"
}
function dbpush() {
adb push "/Users/username/Desktop/$1.db" "/sdcard/db/tmp/"
}
function dbpush2() {
adb shell run-as "com.$1.debug" cp "/sdcard/db/tmp/$2.db" "/data/data/com.$1.debug/databases/$2.db"
}
function dbcheck() {
adb shell run-as "com.$1.debug" ls -al "/data/data/com.$1.debug/databases/"
}
Just write above code lines in your .bash_profile and then call it in terminal.
dbpull myapp mydata
At this moment I prefer to use Visual Studio Code than Android Studio to develop my Android apps. So, I need to know more about commands in terminal, e.g. adb, gradle, etc.
I hope this would be useful for everyone.
Use this command
adb push <file_path> <android_device_path>
adb pull <android_device_path>
I am trying to access adb shell of an android device and pass multiple commands such as
adb root
adb shell
cd /data/local/tmp
export PATH =/data/local/tmp:$PATH
export PATH =/data/local/tmp/testcases:$PATH
sh ./runltp -p -l 1213reportipc.log -d /data/local/tmp/LTP13/tmp -f ipc1q
exit
While I am able to do this manually using adb shell, I am unable to do this via a Python script since as soon script execution encounters 'adb shell' command it spawns a new internal shell and rest of the commands are not executed.
Can anyone please help me with this.
Use a text editor such as sublime or something, write your full adb script using subprocess to send commands and run it while your laptop is connected to your phone and USB debugging is switched on.
import subprocess
subprocess.call("adb shell <your command here>", shell = True)
subprocess.call("adb shell tap 172 3241", shell = True) -- example
etc, just put your commands after the 'adb shell' and run the python script. This should type all your commands into the adb shell. You should even be able to enter commands such as
subprocess.call("adb kill-server", shell=True)
or subprocess.call("adb root", shell=True)
I want to know whether it is possible to run /system/bin/sh android shell commands remotely from windows by writing a batch file or any other way.
when I write a batch file it's not executing any commands after "adb shell" (the control is shifting to /system/bin/sh and I cannot run any commands from here)
what I need is to know if there is a way to give commands to this shell running on my android device without typing them manually ?
Simple example of sending the following lines to the input buffer for adb shell to process.
#echo off
(
echo ls
echo cd sdcard
echo ls
echo exit
) | adb shell
I'm using an Android-x86 vm running in vmware fusion for some testing purposes. I am pushing files to it via adb push, however, every time I uninstall and reinstall the app, I have to execute the following:
adb shell
su
chmod 777 /my/path
exit
exit
I need to be able to script out this entire process but I'm not sure how I can manage the chmod process via some sort of script. I tried doing something like
adb shell am chmod 777 /my/path
But that doesn't work. I thought it would because I can do
adb shell am start ...
I also tried:
adb shell "su && chmod 777 /my/path && exit && exit"
which actually works, but doesn't exit the shell process. So any advice is much appreciated.
You can pass commands to adb shell, as you mentioned, though it works better if you surround the command in quotes.
Likewise, you can pass commands to the 'su' command using the -c argument. Add all of this together and it should do what you want in a nice one-liner!
adb shell "su -c 'chmod 777 /my/path'"
One problem with ADB is that you need multiple commands to get things done.
For example:
adb shell
su
cp /data/local/x /data/local/y
exit
adb pull /data/local/y
Can this be done using python popen and os-system? Tried the example below without success..
print 'Starting emulator...'
subprocess.Popen(['emulator', '-avd', 'testavd'])
os.system('adb wait-for-device')
os.system('Perform whatever adb commands you need')
Any pointers?
You can simply do:
adb shell su -c cp /data/local/x /data/local/y
adb pull /data/local/y
or, if you want to run more than one command (only Linux & OSX):
adb shell <<EOF
ls
date
cat /proc/version
exit
EOF