2

I'm trying to create a script to find and remove my app from the Android emulator through the adb shell.

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. 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. Although if I add an "ls", it prints the contents of the folder correctly... So;
  • echo $app prints nothing (an empty string) and;
  • echo 'bundle name is $bundle' prints bundle name is.

Therefore, it obviously never goes inside my if block, falling in to my else clause and that's it.

What am I doing wrong? I'm not very experienced in shell script, I'd appreciate any ideas.

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. Other ideas are also very much welcome.

Thanks!

2
  • 1
    You need to "invert" the quotes. Please use ' instead of " and " instead of ' in your example and it should work Commented Nov 11, 2014 at 17:33
  • It is more beneficial to state what you want to ultimately achieve with your code (and put that into the title) instead of limiting the help to troubleshooting a very suboptimal implementation attempt. Commented Nov 11, 2014 at 18:51

2 Answers 2

2

You should not really go through the /data/app folder. If you want to uninstall multiple packages with names matching the com.mycompany pattern with a single adb command use:

adb shell "pm list packages com.mycompany | cut -c9- | xargs -n 1 sh /system/bin/pm uninstall"
Sign up to request clarification or add additional context in comments.

2 Comments

Alex, thanks for the tip on the single-line command. Works like a charm, so I'm marking it as the answer. However, this doesn't answer my question of "what am I doing wrong?". I'm still curious to know why my approach was "suboptimal" and what could I have done better? Thanks.
I was just referring to the needlessly overcomplicated way of finding out which packages were installed. "inverting" quotes alone was not enough to fix your code - so it was easier to scrap it completely
1

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. Variables can not be inside ticks '...$VAR' but "...$VAR". Anything inside ticks is taken "as is", i.e. literally:

echo 'bundle name is $bundle'

vs.

echo "bundle name is $bundle"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.