开发者

adb uninstall command doesn't work in Bash shell for statement [duplicate]

开发者 https://www.devze.com 2023-04-04 19:23 出处:网络
This question already has answers here: Read binary stdout data like screencap data from adb shell? (19 answers)
This question already has answers here: Read binary stdout data like screencap data from adb shell? (19 answers) Closed 5 years ago.

For work reason, I need to uninstall and reinstall several a开发者_如何学Gopps in Android device. To do it quickly, it's easy for me to write a script to do this so that I don't need to type it again and again. Here is my script:

#!/bin/sh
#adb uninstall com.company.myapp

for i in `adb shell pm list packages|grep company|awk -F':' '{ print $2 }'`; do
    echo adb uninstall "$i"
    adb uninstall "$i"
done

The odd thing is `adb' always prints "Failure" while 'echo' prints the right command. The second line will work fine if uncomment it.

Anyone can help to figure out what the problem is?


'Failed' ... i used dos2unix to get around this same issue, but like tr -d "\r" better. Not every adb shell command appends the MSDOS CRLF, but only some some of the time.

adb uninstall failed


#!/bin/bash
# By ESSPEE
# Initial Build: 23rd Dec, 2015

(
app=$(zenity --entry --text='Enter the application name (whatever you remember) :' --entry-text='facebook'  --height=100 --width=450 --title="Uninstall Android Application");
filter=$(adb shell pm list packages | cut -d":" -f2 | grep -i "$app" | dos2unix);
counter=$(echo "$filter" | wc -l);


if [[ "`adb shell pm list packages | cut -d":" -f2 | grep -i "$app"`" == "" ]]; then
zenity --error --title="Uninstall Android Application" --text="No such application installed on the android device.\nEither it is not installed or you don't have permission.\n\n<b>=> It might be possible that you want to uninstall system\napplication and your device is not rooted.</b> " --no-wrap
exit 1 
fi

if [[ "$counter" -eq 1 ]]; then
zenity --question --title="Application to be uninstalled" --text="You have selected $counter package to be uninstalled.\nDo you really want to uninstall :-\n\n<b><i>$filter</i></b>" --no-wrap
if [[ $? == 0 ]]; then
  adb shell pm disable $filter
  adb shell pm clear $filter
  adb shell pm uninstall $filter
  echo "10" ; sleep 1
  echo "# $counter application being uninstalled from android device ..." ; sleep 1

else
  exit 1 
fi
elif [[ "$counter" -gt 1 ]]; then
zenity --question --title="$counter Application to be uninstalled" --text="<b>NOTICE:</b>  You have selected $counter applications to be uninstalled.\nDo you really want to uninstall the following packages :-\n\n<b><i>$filter</i></b>" --no-wrap
  if [[ $? == 0 ]]; then
  echo "10" ; sleep 1
  echo "# $counter Android applications being uninstalled from android device ..." ; sleep 1
  for file in $filter ; 
  do 
    adb shell pm disable $file;
    adb shell pm clear $file;
    adb shell pm uninstall $file; 
  done
else
  exit 1  
fi
fi

notify-send --icon=/usr/share/icons/SSB/scalable/apps/apk.png "Uninstall Android Application" "$counter Applications uninstalled."

echo "100" ; sleep 1
) |
zenity --progress --pulsate --auto-close --title="Uninstall Android Application" --text="Application being uninstalled from android device ..." --width=500 --height=100

exit 0


Is necessary dos2unix command, in the next example I'll show implementation:

#!/bin/bash
####################################################################
# ADB Uninstall Util (GPL license, author: @hpsaturn)
#
# Dependencies: android-tools-adb, dos2unix
#
# Revision:
# -----------------------------------------------------------------
# 20171005 get package name (without execution)
# 20171019 filter for multiple apks and execution ready
####################################################################

pkg=`adb shell 'pm list packages -f' | sed -e 's/.*=//' | grep $1`
cnt=`echo "$pkg" | wc -l`
adb=`echo "$pkg" | sed -e 's/^/adb uninstall /'`
cmd=`echo "$adb" | dos2unix`

if [ "$pkg" = "" ]; then
    echo "package not found!"
    exit 1
elif (( $cnt > 1 )); then
    echo ""
    echo "multiple packages found:"
    echo ""
    echo "$pkg"
    echo ""
else
    echo "uninstalling: $pkg"
    bash -c "$cmd"
fi
0

精彩评论

暂无评论...
验证码 换一张
取 消