【问题标题】:How to use su command over adb shell?如何在 adb shell 上使用 su 命令?
【发布时间】:2015-02-01 04:05:37
【问题描述】:

我需要制作一个在Android设备上执行很多事情的脚本,我的设备是root的,当我进入shell时,我可以给出命令su,它可以工作,但我需要传递这个命令:

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

当我在 su 之前放置一些命令时,它会根据我读到的内容创建一个新的 shell 立即返回,但是我需要在 su shell 上给出命令,我该怎么做?

【问题讨论】:

  • su -c 将根据 su 的参数调用命令。
  • 如果您需要在模拟器上拥有 root 访问权限 - 请查看 *.com/questions/43923996/…

标签: android linux shell android-ndk adb


【解决方案1】:

我选择以下:

adb shell su root <your command>

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

adb shell su root ls storage/0/emulated

【讨论】:

    【解决方案2】:

    1。 adb shell 苏

    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
    

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

    【讨论】:

      【解决方案3】:

      好吧,如果您的手机已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 中运行时的正常方式完全相同。

      【讨论】:

      • 对我来说,您的解决方案不起作用,而是 adb shell "su 0 command" 起作用,这是一个示例 adb shell "su 0 chmod 644 /system/priv-app/myApp.apk"
      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

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

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

        解决方案在 Linux 上

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

        【讨论】:

          【解决方案6】:

          默认情况下,CM10 仅允许来自应用程序而非 ADB 的 root 访问。转到设置 -> 开发人员选项 -> Root 访问权限,然后将选项更改为“应用程序和 ADB”。

          【讨论】:

            【解决方案7】:

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

            试试adb shell su -c YOUR_COMMAND

            【讨论】: