Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch a script as root through ADB

I have created a script to mount partitions and do some stuff in my Android system. I saved the script as install.sh in the /bin folder of Android.

I want to call the script from ADB, which is itself called from a batch file on Windows, but it needs to be executed as root.

The first solution I tried was to call the script using

adb shell "su -c sh /bin/script.sh" 

but it does not work as it gives me a shell access (with root permissions), but nothing is executed. I also tried to call

adb root "sh /bin/script.sh" 

but I got the following error

adbd cannot run as root in production builds 

I then tried to write

su -c "command" 

for all the commands which need a root access in my script, but I have the same problem. When I run the script I only obtain a root shell and nothing is executed.

If I use the first solution by hand (e.g. I call adb shell su, then my script), it works. However the whole point is to automate the process, so that adb shell can be called from another script.

Do you have any idea of how I could achieve this ?

Thanks !

like image 297
ErGo_404 Avatar asked Jan 06 '12 17:01

ErGo_404


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 .


2 Answers

This works for me:

Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):

adb shell "su -c 'command1; command2; command3'" 

then run myscript.bat from a DOS shell.

Note: it doesn't appear that the the DOS line continuation character (^) works in this situation. In other words, the following doesn't work for me:

adb shell "su -c '^ command1; ^ command2; ^ command3'" 

This results in "Syntax error: Unterminated quoted string"

like image 151
Andy Dennie Avatar answered Sep 28 '22 10:09

Andy Dennie


This works :

adb shell echo command which needs root privileges \| su 

If you need redirection:

adb shell echo 'echo anytext > /data/data/aforbiddenfolder/file' \| su 

For "copying" a local file to an android path needing root privileges (but alocalfile must not contain '):

cat alocalfile | adb shell echo "echo '`cat`' > /data/data/aforbiddenfolder/file" \| su 

If you have a better way (even for su versions which don't have -c), I am interested.

like image 37
rom1v Avatar answered Sep 28 '22 09:09

rom1v