繁体   English   中英

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

[英]How to use su command over adb shell?

我需要制作一个在 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 上给出命令,我该怎么做?

好吧,如果您的手机已植根,您可以使用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 中运行时通常使用的方式输入命令。

su命令不执行任何操作,它只是提高您的权限。

尝试adb shell su -c YOUR_COMMAND

在我的 Linux 上,我看到一个错误

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

su: 无效的 uid/gid '-c'

解决方案是在 Linux 上

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

默认情况下,CM10 只允许从应用程序而不是 ADB 进行 root 访问。 转到设置 -> 开发人员选项 -> Root 访问权限,并将选项更改为“应用程序和 ADB”。

对于我的用例,我想从 magisk 配置文件中获取 SHA1 哈希值。 以下为我工作。

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

1. adb shell su

赢得 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

赢得 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 CMD, -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 [-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 调试桥版本 1.0.41
版本 30.0.5-6877874

我选择以下内容:

adb shell su root <your command>

例如,访问外部存储(sd 卡):

adb shell su root ls storage/0/emulated

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM