OP 尝试将以下 3 个命令(他在交互式 shell 会话中依次执行没有问题)组合成一个非交互式命令:
adb shell
run-as com.example.app
cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
为了简单起见,让我们从交互式内部开始adb shell
会议。如果我们只是尝试将最后两个命令合并到一行中:
run-as com.example.app cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml
由于 shell 重定向的工作方式,这将不起作用 - 只有cat /sdcard/temp_prefs.xml
部分命令将运行com.example.app
UID
很多人"know"将命令中有关重定向的部分放入引号中:
run-as com.example.app "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
这不起作用,因为run-as
命令不够智能,无法解析整个命令。它期望一个可执行文件作为下一个参数。正确的方法是使用sh
反而:
run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"
那么我们可以在前面加上adb shell
听从命令并完成它?不必要。通过从您的 PC 运行该命令,您还可以添加另一个本地 shell 及其解析器。具体转义要求取决于您的操作系统。在 Linux 或 OSX 中(如果您的命令尚未包含任何'
)很容易单引号整个命令,如下所示:
adb shell 'run-as com.example.app sh -c "cat /sdcard/temp_prefs.xml > shared_prefs/com.example.app_preferences.xml"'
但有时使用带有(-out或更少)引号的替代解决方案更容易:
adb shell run-as com.example.app cp /sdcard/temp_prefs.xml shared_prefs/com.example.app_preferences.xml
或者如果您的设备没有cp
命令:
adb shell run-as com.example.app dd if=/sdcard/temp_prefs.xml of=shared_prefs/com.example.app_preferences.xml
另请注意我是如何使用的shared_prefs/com.example.app_preferences.xml
而不是完整的/data/data/com.example.app/shared_prefs/com.example.app_preferences.xml
- 通常在里面run-as
命令你当前的目录是HOME
你的包的目录。