Running a shell script in Android from the Host - android

I have a Host development PC running Ubuntu and I am doing all the android development on it.From this PC I wanted to (adb) push some files(executables) to android filesystem (say /data/dir1) , cd into it and run that executable.Using a shell script (shown below) I can do this from the PC upto to connecting the android device and doing adb shell but I can not run other commands after that .
e.g scripts
adb push <file1> /data/dir1/
adb shell
cd data/dir1
./file1
I can run upto adb shell but not beyond that.How can I achieve the remaining two commands ( changing the directory to data/dir1 and running the ./file1) from the shell script running on the Host PC.

You
don't need to enter into the adb shell
, change the path and more.
You can accomplish everything in a single command , like
adb push my_script_file.sh /data/dir1/
adb shell sh data/dir1/my_script_file.sh

1.first of all go the root by the command sudo -i
write the program
adb shell ls data/dir1
3.execute it -./file
by the above program you can move to the folder in the shell

Related

How to write adb commands into a bat

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.

Android Studio adb on Ubuntu

I try to connect the device from USB to Wifi but the command prompt tells me all the time that adb is not found.
But it is definitly inside my folder: "~/Android/Sdk/platform-tools"
it contain:
adb e2fsdroid hprof-conv mke2fs package.xml sqlite3
api etc1tool lib64 mke2fs.conf sload_f2fs systrace
dmtracedump fastboot make_f2fs NOTICE.txt source.properties
I start with
adb tcpip 5555
Here I get the error message. What I do wrong here?
Your shell doesn't know where to look when you call adb that's why you need to specify it.
First check which shell you're using by calling echo $SHELL - the usual ones on Linux are bash and zsh. For Bash the config file is in ~/.bash_profile, for zsh it's in ~/.zshrc.
Depending on what shell you're using open that file and add this line into it:
export PATH=${PATH}:$HOME/Android/Sdk/platform-tools/
And restart the shell.
Then you can check that you the adb location is recognized with:
adb --version

How do I run "adb shell" commands in a terminal emulator locally on an Android device?

From a shell on my PC, I can run adb shell cmd package list packages, and get a list of all installed packages. I would like to run this and similar commands locally on my Android phone (Nexus 6P) in a terminal emulator (currently using Termux).
If I open the same shell with /system/bin/sh, and then try to run /system/bin/cmd package list packages, nothing happens (no errors, just outputs nothing and reloads the prompt).
If I run /system/bin/cmd -l the list of options appears as expected. $PATH and $LD_LIBRARY_PATH are the same in both environments. One major difference is that echo $USER returns "shell" from adb shell, but returns my local username from /system/bin/sh launched from Termux.
Is there any way to replicate the behavior of commands run from adb shell in a terminal emulator locally on Android?
Edit:
My device is rooted, and I am OK with root only solutions.
The problem is Termux. By design, Termux runs only (or is mostly?) the Linux command line programs that you install from within Termux using apt or the newer "native" package management interface, e.g. apt install bsdtar. What you need to run the adb shell commands is a terminal emulator that can truly access the underlying Android file system, not just the Termux that is practically a chroot save for the fact that it's aware it's not running commands from the filesystem root /.
As a simple test, run the following command:
which ls
It should return something like /system/bin/ls. But if it returns something like /data/data/com.termux/files/usr/bin/applets/ls then you have to change your terminal emulator to something else. I suspect that Termux was designed to take into account the more restrictive shell execution policies that Google put into place after KitKat or the Android 4.X.
The Android distribution I'm using, LineageOS 14.1, comes with a built-in shell emulator that allows me to run commands found in /system/bin/ls.
I don't have a rooted Nougat device handy, but something like the following may be a close enough approximation to adb shell (assuming you are using SuperSU):
env -i USER=shell "$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su)" shell --context u:r:shell:s0 --shell /system/bin/sh --command COMMAND
I (very briefly) tested it from Termux on a rooted Marshmallow device.
To elaborate:
the -i flag is used to start with an empty environment
USER=shell isn't specifically required, but for some reason su refuses to run with a completely empty environment
$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su) points to the full path of the su binary on your device and can be hardcoded if you prefer
shell instructs the su binary to login as the shell user (the same as adb shell)
--context u:r:shell:s0 sets the appropriate SELinux context
--shell /system/bin/sh instructs SuperSU to use the system shell rather than it's own sush shell
Another option would be to actually run adb from the device, connecting to itself over TCP. If you need some functionality that is only available via adb (e.g. in my case it was adb forward) then this may be your only option. Unfortunately this isn't particularly convenient.
I wasn't able to find success with any publicly available adb binaries, so I build it myself with a few minor changes. You can see the sources I used and the changes I made at https://github.com/shakalaca/fastboot-adb-android and https://github.com/brbsix/fastboot-adb-android, respectively.
Once you have adb installed, here's an abbreviated list of commands I used to connect to the device:
# Add iptables rules to block external connections to port 9999'
su root iptables -N adbd
su root iptables -A adbd -i lo -p tcp -m tcp --dport 9999 -j ACCEPT
su root iptables -A adbd -p tcp -m tcp --dport 9999 -j DROP
su root iptables -A INPUT -j adbd
# Necessary in order to display authorization prompt
su shell setprop ro.debuggable 1
su shell setprop service.adb.tcp.port 9999
su root start adbd
adb connect 127.0.0.1:9999
adb wait-for-local-device
To shut down:
adb kill-server
su root stop adbd
su shell setprop ro.debuggable 0
su shell setprop service.adb.tcp.port 0
su root iptables -D INPUT -j adbd
su root iptables -F adbd
su root iptables -X adbd
So I tried this recently...if you're rooted you can use a terminal emulator.
su
then the command you want without "adb shell" part of it.
i tried the command "adb shell dumpsys deviceidle force-idle" in order to force device into doze.
I did this on the device via terminal emulator as:
"dumpsys deviceidle force-idle" and it did take effect.
also the dumpsys batterystats command worked.
be careful with commands with extensive text output, as the screen will be flooded with the output and will be unresponsive for some time.
EDIT
I originally answered this without the termux tag in mind. This worked for me while trying to execute shell commands on a vanilla emulator and saw this question while researching, so I tried to answer it differently.
You almost had it there in your question. You only need to execute sh:
int result = -1;
try {
final Process shell = Runtime.getRuntime().exec("sh");
final DataOutputStream commands = new DataOutputStream(shell.getOutputStream());
commands.writeBytes("write a series");
commands.writeBytes("of commands here");
commands.writeBytes("exit\n");
commands.flush();
result = shell.waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
If result == 0 the commands were succesful, else otherwise
Only rooted android
Busybox must be installed (though you can try without it)
Just write the normal command without the prefix adb

Android Debug Bridge adb shell

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

Start a process in background from adb shell without attaching the process to the terminal in Android

I have a simple problem.
I want to start/run a program file on an android phone using adb shell.
My Program is in /system/bin folder and has root privileges.
I want to run this program from my command prompt as adb shell runme 3000000 > logs.txt but it should not block the terminal, It should run in background.
I cannot use screen/disown/nohup for my problem as android doesn't have all this.
I tried
adb shell "runme >logs.txt &" but of no use.
When i issue command as
adb shell
# runme 3000000 > logs.txt &
It runs fine, when i exit the terminal/disconnect the device and then connect again to system.
Do adb shell ps | grep runme shows the process is still runnning in background.
Thanks
Busybox has nohup applet which works just fine in Android

Categories

Resources