如何在adb shell中使用su命令?

32

我需要制作一个脚本,在Android设备上执行许多操作。我的设备已经获取了root权限,当我进入shell时,我可以使用命令su,它可以工作,但是我需要像这样传递这个命令:

adb shell "
su;
mv /sdcard/Download/app_test /data/local;
cd /data/local;
./app_test;
exit;
exit;
"

在su命令之前加入一些命令是起作用的,根据我所读的,su创建一个立即返回的新shell,但我需要在su shell上执行命令,我该怎么做?


2
su -c 将根据 su 的参数调用一个命令。 - Marc B
如果您需要在模拟器上获得root访问权限,请查看https://dev59.com/JlcP5IYBdhLWcg3wu8a1#45668555。 - Robert Lujo
7个回答

33

如果您的手机已经取得了root权限,您可以使用su -c命令来运行命令。

下面是一个在build.prop文件上运行cat命令以获取手机产品信息的示例。

adb shell "su -c 'cat /system/build.prop |grep "product"'"

这会调用root权限并在' '中运行命令。

请注意使用5个尾引号,这是必要的,否则将出现错误。

为了澄清,格式如下。

adb shell "su -c '[your command goes here]'"

确保您在运行命令时以与在shell中运行时相同的方式输入命令。


3
你的解决方案对我无效,相反,adb shell "su 0 command"有效,并且这里有一个示例:adb shell "su 0 chmod 644 /system/priv-app/myApp.apk"。请注意,我的翻译尽力保持原文意思不变,同时使语言更加通俗易懂。 - Alireza Ahmadi
1
如果我需要输入的命令已经有了 ",该怎么办?例如,我需要运行此命令以执行 SwittBackup 应用程序的定期备份(https://swiftapps.org/faq#schedulecommand):`am start -n org.swiftapps.swiftbackup/.shortcuts.ShortcutsActivity -e "cmd" "-s vmppnc"。我该如何正确地将其传递给 adb shell "su`? - android developer

13

在我的Linux上,我看到一个关于

的错误。

adb shell "su -c '[your command goes here]'"

su: 无效的 uid/gid“-c”

在Linux上的解决方法

adb shell su 0 '[your command goes here]'

1
遇到了同样的问题,然后输入 su 0 就解决了。谢谢! - seriouslysupersonic

7
su命令并不执行任何操作,它只是提升您的权限。 请尝试使用adb shell su -c YOUR_COMMAND

2
su: invalid uid/gid '-c' - alizeyn

4

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

如果想要运行am命令,可能需要使用-t选项:

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选项:

 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

安卓调试桥版本 1.0.41
版本号 30.0.5-6877874


2

默认情况下,CM10仅允许应用程序而不是ADB访问root权限。请前往“设置” -> “开发者选项” -> “Root访问”,并将选项更改为“应用程序和ADB”。


1
我愿意选择以下内容:
adb shell su root <your command>

例如,访问外部存储(SD卡)的方法如下:
adb shell su root ls storage/0/emulated

1

根据我的使用情况,我想从Magisk配置文件中获取SHA1哈希值。以下方法适用于我。

adb shell "su -c "cat /sbin/.magisk/config | grep SHA | awk -F= '{ print $2 }'""

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接