Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use su command over adb shell?

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?

like image 380
Alex Avatar asked Dec 03 '14 14:12

Alex


People also ask

How do I switch to root in adb?

If you really need to have ADB running as root , the quickest and easiest way is to install Android Custom ROMs and the most popular is CyanogenMod for it has the Root Access options in developer options menu where you can choose to give root access to apps and ADB .

How do I run a batch file in adb shell?

Put adb shell > nameofyourtxt. txt. If the only command you want to execute is "su", you can do adb shell su.


3 Answers

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.

like image 71
A-Droid Tech Avatar answered Oct 21 '22 18:10

A-Droid Tech


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]'
like image 11
hannes ach Avatar answered Oct 21 '22 19:10

hannes ach


The su command does not execute anything, it just raise your privileges.

Try adb shell su -c YOUR_COMMAND.

like image 7
shkschneider Avatar answered Oct 21 '22 17:10

shkschneider