27

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 !

ErGo_404
  • 1,801
  • 4
  • 18
  • 22

7 Answers7

59

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"

Andy Dennie
  • 6,012
  • 2
  • 32
  • 51
  • This works amazingly on my Samsung Note 4 Exynos version, system version Android 4.4. I have searched online for solutions for the whole day. Thank god, I finally read this post. It works. The RootAdb used to work on Galaxy Note 10.1 tablet, but it doesn't work for this Note4 any more. – Robert Wang Feb 05 '15 at 05:50
  • on Android 7.1.1 I found that using the single quotes within the double quotes actually messed up the execution. Removing them seemed to make everything work as expected. – MikeSchem Jan 10 '17 at 17:19
4

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.

rom1v
  • 2,752
  • 3
  • 21
  • 47
  • When I run command with output redirection like this `adb shell 'echo uname -a > /ggg.txt' \| su` I get error that filesystem is readonly, but I can run same command succesfully when I log in as root by `adb shell su`. Is there workaround for this issue? – Shadasviar Feb 02 '19 at 20:18
2

This works for me:

adb shell "su -c ./data/local/tcpdump-arm -s 0 -v -w /data/local/appxpress_dump.pcap"
slhck
  • 36,575
  • 28
  • 148
  • 201
tn.stackoverflow
  • 367
  • 1
  • 3
  • 6
1

I am not sure if I provided a solution or asked for a better one. I wanted to run some 200 command in batch mode to be sent to adb I followed this approach

    adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "
adb shell "su -c command ; "

and I saved them in a batch file

This command

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

will not work beyond a certain max size . It did not work

error: service name too long
user1874594
  • 2,277
  • 1
  • 25
  • 49
0

Working solution with Android API 32 emulator:

adb shell "su root 'command'"

For some reason all other methods do not work (or return errors).

Alex SSB
  • 1
  • 2
0

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

How do you know that you are given root permissions? I assume you are attempting to execute the script on a device? Has your device been rooted?

You may need to give execute permissions via chmod to the file.

chmod ugo=rwx /bin/script.sh
DavidDraughn
  • 1,110
  • 1
  • 8
  • 15
  • Nothing is executed, but the console displays a shell were I can type my commands, with "#", meaning that I have a root access. And yes, my device is rooted. My script also has the execute permissions, but calling sh /bin/script.sh should not need this permission, right ? – ErGo_404 Jan 08 '12 at 13:11
  • Ah, I didn't even realize it, but you're correct about not needing to have execute permissions. You could try 'source /bin/script.sh' and see if that is any different (probably not). – DavidDraughn Jan 20 '12 at 22:08
0

It appears that I was using a very simple version of su which did not accept the -c argument. I copied another su which did work. AndyD is totally right though, so I am accepting his answer instead of mine :)

ErGo_404
  • 1,801
  • 4
  • 18
  • 22