通过 ADB 以 root 身份启动脚本
我创建了一个脚本来挂载分区并在我的 Android 系统中执行一些操作。我将脚本保存为Android的/bin文件夹中的install.sh。
我想从 ADB 调用脚本,它本身是从 Windows 上的批处理文件调用的,但需要以 root 身份执行。
我尝试的第一个解决方案是使用调用脚本
adb shell "su -c sh /bin/script.sh"
,但它不起作用,因为它为我提供了 shell 访问权限(具有 root 权限),但没有执行任何操作。 我也尝试打电话
adb root "sh /bin/script.sh"
,但出现以下错误,
adbd cannot run as root in production builds
然后我尝试在脚本中编写
su -c "command"
所有需要根访问权限的命令,但我遇到了同样的问题。 当我运行该脚本时,我只获得一个 root shell,并且没有执行任何操作。
如果我手动使用第一个解决方案(例如,我调用 adb shell su,然后调用我的脚本),它就可以工作。然而,重点是使该过程自动化,以便可以从另一个脚本调用 adb shell。
您知道我如何实现这一目标吗?
谢谢 !
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这对我有用:
创建 myscript.bat 并将其放入其中(注意要在超级用户模式下执行的命令周围的单引号):
然后从 DOS shell 运行 myscript.bat。
注意:在这种情况下,DOS 行继续符 (^) 似乎不起作用。换句话说,以下内容对我不起作用:
这会导致“语法错误:未终止的引号字符串”
This works for me:
Create myscript.bat and put into it (note the single quotes around the commands to be executed in superuser mode):
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:
This results in "Syntax error: Unterminated quoted string"
这有效:
如果您需要重定向:
将本地文件“复制”到需要root权限的android路径(但本地文件不得包含
'
):如果您有更好的方式(即使对于没有
-c
的su
版本),我很感兴趣。This works :
If you need redirection:
For "copying" a local file to an android path needing root privileges (but alocalfile must not contain
'
):If you have a better way (even for
su
versions which don't have-c
), I am interested.这对我有用:
This works for me:
我不确定我是否提供了解决方案或要求更好的解决方案。
我想以批处理模式运行大约 200 个命令发送到 adb
我遵循了这种方法
,并将它们保存在批处理文件中。
该命令
超过特定的最大大小, 将无法工作。没用
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
and I saved them in a batch file
This command
will not work beyond a certain max size . It did not work
您如何知道您已获得 root 权限?我假设您正在尝试在设备上执行脚本?您的设备已经root了吗?
您可能需要通过 chmod 向该文件授予执行权限。
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.
看来我使用的是一个非常简单的 su 版本,它不接受 -c 参数。
我复制了另一个有效的 su 。 AndyD 是完全正确的,所以我接受他的答案而不是我的:)
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 :)
使用 Android API 32 模拟器的工作解决方案:
由于某种原因,所有其他方法都不起作用(或返回错误)。
Working solution with Android API 32 emulator:
For some reason all other methods do not work (or return errors).