How to use su command over adb shell? - android

I need to make a script that executes a lots of thing on Android device, my device is rooted, when I enter on the shell, I can give the command su, and it works but I need pass this command like:
adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"
when I put some commands before the su it works, according what I read su creates a new shell that return immediately, but I need give commands on the su shell, how can I do that?

Well, if your phone is rooted you can run commands with the su -c command.
Here is an example of a cat command on the build.prop file to get a phone's product information.
adb shell "su -c 'cat /system/build.prop |grep "product"'"
This invokes root permission and runs the command inside the ' '.
Notice the 5 end quotes, that is required that you close ALL your end quotes or you will get an error.
For clarification the format is like this.
adb shell "su -c '[your command goes here]'"
Make sure you enter the command EXACTLY the way that you normally would when running it in shell.

On my Linux, I see an error with
adb shell "su -c '[your command goes here]'"
su: invalid uid/gid '-c'
The solution is on Linux
adb shell su 0 '[your command goes here]'

The su command does not execute anything, it just raise your privileges.
Try adb shell su -c YOUR_COMMAND.

By default CM10 only allows root access from Apps not ADB. Go to Settings -> Developer options -> Root access, and change option to "Apps and ADB".

1. adb shell su
win cmd
C:\>adb shell id
uid=2000(shell) gid=2000(shell)
C:\>adb shell 'su -c id'
/system/bin/sh: su -c id: inaccessible or not found
C:\>adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
C:\>adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
win msys bash
msys2#bash:~$ adb shell 'su -c id'
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell "su -c id"
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
msys2#bash:~$ adb shell su -c id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
2. adb shell -t
if want run am cmd, -t option maybe required:
C:\>adb shell su -c am stack list
cmd: Failure calling service activity: Failed transaction (2147483646)
C:\>adb shell -t su -c am stack list
Stack id=0 bounds=[0,0][1200,1920] displayId=0 userId=0
...
shell options:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable pty allocation
-t: allocate a pty if on a tty (-tt: force pty allocation)
-x: disable remote exit codes and stdout/stderr separation
Android Debug Bridge version 1.0.41
Version 30.0.5-6877874

for my use case, i wanted to grab the SHA1 hash from the magisk config file. the below worked for me.
adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""

I opt for the following:
adb shell su root <your command>
e.g., to access the external storage (sd card):
adb shell su root ls storage/0/emulated

Related

adb shell su -c command throws Permission Denied (My phone is rooted)

First of all, I'm experimenting a DVFS with my rooted phone, Galaxy A12. Nevertheless, if I use a command inside of adb shell then it does not throw a Permission Denied.
~$ adb shell
a12:/ $ su
a12:/ # echo 500000 > sys/kernel/gpu/gpu_max_clock
a12:/ #
However, when I use this command outside of android kernel with adb shell su -c, it throws a Permission denied
~$ adb shell su -c "echo 680000 > sys/kernel/gpu/gpu_max_clock"
/system/bin/sh: can't create sys/kernel/gpu/gpu_max_clock: Permission denied
I've tried some commands like the below, however the commands threw the same thing.
adb shell su -c "chmod 755 sys/kernel/gpu/gpu_max_clock && echo 680000 > sys/kernel/gpu/gpu_max_clock"
adb shell su -c "chmod 777 sys/kernel/gpu/gpu_max_clock && echo 680000 > sys/kernel/gpu/gpu_max_clock"
How can I handle this situation?
A shell terminal does not throw any exceptions and your command line is wrong - the path, too:
adb shell "su -c 'echo 680000 > /sys/kernel/gpu/gpu_max_clock'"
You might end up with a defect GPU, when not having the slightest clue of what you're doing.
Better research the max values first - when it goes up in smoke, this is your own fault, not mine.
Any sane person would probably read the original clock speed first:
cat /sys/kernel/gpu/gpu_max_clock
Only then one can see, how many percent of the original clock speed 680000 would even be.
When giving to much, this will lead to overheating and random crashes... unless also reducing the voltage. This is by far not as easy as you may think it is ...and there is no active cooling available, either.
Your command line is wrong. Try this.
adb shell "su -c 'echo 680000 > /sys/kernel/gpu/gpu_max_clock'"
or
adb shell "su -c 'chmod 777 /sys/kernel/gpu/gpu_max_clock && echo 680000 > /sys/kernel/gpu/gpu_max_clock'"

adb.exe: unknown command am adb.exe: unknown command sleep error keeps poping up when am trying to run the shell script on my rooted android device

Hi guys am new to the shell scripting.. please do not mind if acted like a noob.
The following error keeps poping up when am trying to run the shell script on my android device
#!/bin/bash
for i in $(seq 100)
do
echo "Time: $i"
adb -s $* shell am start com.android.camera/.Camera -W -S
adb -s $* shell sleep 3
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
adb -s $* shell input tap 540 1994
count=`adb -s $* shell ls /sdcard/DCIM/Camera/ | wc |awk '{print $1}'`
echo Count: $count
if [[ count -gt 500 ]]; then
break
fi
adb -s $* reboot
adb -s $* wait-for-device
adb -s $* shell sleep 40
adb -s $* shell input tap 500 1200
done
Result:
adb.exe: unknown command am
adb.exe: unknown command sleep
you can't run adb commands on your android device. instead the adb binary must installed on host. the script is a linux script and can not run from windows. prepare bootable usb flash drive and run this script from any linux terminal
furthermore some of your commands requires root permissions. therefore sush must invited (and permissions granted on superuser app on touchscreen)
adb shell su -c "am start com.android.camera/.Camera -W -S"
awk is not available on android. to make it more clear that these pipes are actually running on host, quote the android commands (or even better avoid awk at all)
count=$(adb shell "ls -1 /sdcard/DCIM/Camera" | wc | awk '{print $1}')
count=$(adb shell "ls -1 /sdcard/DCIM/Camera | wc -l")
am is actually a shell script pointing to am.jar this requires full booted android. if you try to run in recovery mode you should at least run the framework

How to start daemon on android by adb

Can you help me please, my head is swollen...
I am trying to run daemon on android emu/device by command:
adb -s <device_name> shell su -c /dir/daemon <port_number>
but nothink happens, and no errors!
If I do:
adb -s <device> shell
and then form shell cmdline:
su -c /dir/daemon <port_number>
than its work fine. I try use shell-script and run:
adb -s <device_name> shell sh su -c /dir/script.sh <port_number>
and I even try to generate script from Qt code with port number and use:
adb -s <device_name> shell sh su -c /dir/script.sh
but its not helped... problem that if I enter to adb shell previous to run daemon - its work. Another problem is on different device/emu/iso_image different command format could not work. For example:
adb -s <device_name> shell su -c /dir/daemon <port_number> // work at emu, but not at iso
and vice:
adb -s <device_name> shell su -c "/dir/daemon <port_number>" // work at iso
etc.
Everybody can answer what's the matter?
Sorry for my english
adb shell su -c "/dir > /dev/null 2> /dev/null < /dev/null &"
or
shell su -c /data/local/tmp/start_daemon.sh
#!/system/bin/sh
/data/local/tmp/nohup
/data/local/tmp/daemon <portn_number> &

shell alias has cache? Why?

I write a alias command for get version of android in PC using adb shell, like this:
alias av="echo v:`adb shell getprop ro.build.version.release`;
echo sdk:`adb shell getprop ro.build.version.sdk`;
echo model:`adb shell getprop ro.product.model`;
echo display:`adb shell getprop ro.build.display.id`;"
I connect one device, it right outputs :
v:4.0.4
sdk:15
model:ZTE N983
display:N983V1.0.0B06
But when I change another device. It also output last text. When i open new shell, it output rights again. Why?
Alias has a cache? How to solve it ?
Thanks.
Backticks are evaluated immediately.
Use something like
alias av="echo -n v; adb shell getprop ro.build.version.release; ..."
to make the adb commands be executed at the time of alias execution.
The final result
1 alias av="echo -n v:; adb shell getprop ro.build.version.release; echo -n sdk:;adb shell getprop ro.build.version.sdk; \
echo -n model:;adb shell getprop ro.product.model; echo -n display:;adb shell getprop ro.build.display.id";
2 alias av='echo v: adb shell getprop ro.build.version.release; echo sdk: adb shell getprop ro.build.version.sdk; echo model: adb shell getprop ro.product.model; echo display: adb shell getprop ro.build.display.id'
But The second way cannot begin a new line using \

Running multiple adb commands with python Popen or os.system

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

Categories

Resources