简体   繁体   English

在Android ADB Shell中运行Shell脚本

[英]Running shell script in Android adb shell

I'm trying to create a script to find and remove my app from the Android emulator through the adb shell. 我正在尝试创建一个脚本,以通过adb shell从Android模拟器中查找和删除我的应用。

This is what I've got: 这就是我得到的:

adb shell "
cd data/app
for app in com.mycompany.*.apk;
do
    echo $app
    bundle=$(echo $app | sed 's/-[0-9]//g')
    echo 'bundle name is $bundle'
    if [ '$bundle' != '' ];then
        adb uninstall $bundle
    else
        echo 'No apps found'
    fi
done
exit
"

But it doesn't seem to work as expected. 但是它似乎没有按预期工作。

  • my for loop doesn't iterate through anything. 我的for循环不会遍历任何东西。 If I manually run the commands exactly as above in the shell, it works, but when I run it from a shellscript then the for loop doesn't see the files or anything. 如果我在外壳中完全按照上面的命令手动运行命令,则它可以工作,但是当我从shellscript运行命令时, for循环将看不到文件或任何内容。 Although if I add an "ls", it prints the contents of the folder correctly... So; 尽管如果添加“ ls”,它会正确打印文件夹的内容。
  • echo $app prints nothing (an empty string) and; echo $app输出任何内容(空字符串),并且;
  • echo 'bundle name is $bundle' prints bundle name is . echo 'bundle name is $bundle'打印bundle name is

Therefore, it obviously never goes inside my if block, falling in to my else clause and that's it. 因此,它显然永远不会进入我的if块,而落入我的else子句中。

What am I doing wrong? 我究竟做错了什么? I'm not very experienced in shell script, I'd appreciate any ideas. 我对shell脚本不是很有经验,我会很感激任何想法。

My goal with this is to have a shell function that I can call to automate the process of removing my app from the emulator without having to drag it and uninstall it manually. 我的目标是拥有一个shell函数,可以调用它来自动执行从模拟器中删除应用程序的过程,而无需手动拖动和卸载它。 Other ideas are also very much welcome. 其他想法也非常受欢迎。

Thanks! 谢谢!

You should not really go through the /data/app folder. 您不应该真正浏览/data/app文件夹。 If you want to uninstall multiple packages with names matching the com.mycompany pattern with a single adb command use: 如果要使用单个adb命令卸载名称与com.mycompany模式匹配的多个软件包,请使用:

adb shell "pm list packages com.mycompany | cut -c9- | xargs -n 1 sh /system/bin/pm uninstall"

I'm still curious to know why my approach was "suboptimal" and what could I have done better? 我仍然想知道为什么我的方法“次优”,我能做得更好吗?

Wild guess since I'm not familiar with adb shell but bash: Quotation. 大胆猜测,因为我对adb shell不熟悉,但对bash却很:报价。 Variables can not be inside ticks '...$VAR' but "...$VAR". 变量不能在刻度“ ... $ VAR”内,而应在“ ... $ VAR”内。 Anything inside ticks is taken "as is", ie literally: 滴答中的所有内容均按“原样”进行,即从字面上看:

echo 'bundle name is $bundle'

vs.

echo "bundle name is $bundle"

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

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