adb uninstall命令在Bash shell for语句中不起作用

时间:2011-09-15 05:00:59

标签: android shell adb

出于工作原因,我需要在Android设备中卸载并重新安装多个应用。为了快速完成,我很容易编写一个脚本来执行此操作,这样我就不需要一次又一次地输入它。这是我的剧本:

#!/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

奇怪的是`adb'总是打印“Failure”,而'echo'打印正确的命令。 如果取消注释,第二行将正常工作。

任何人都可以帮忙弄清问题是什么?

3 个答案:

答案 0 :(得分:3)

'失败'...我使用dos2unix来解决同样的问题,但更喜欢tr -d“\ r”。 并非每个adb shell命令都会附加MSDOS CRLF,但有些时候只会附加一些。

adb uninstall failed

答案 1 :(得分:1)

#!/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

答案 2 :(得分:0)

是必要的dos2unix命令,在下一个例子中我将展示实现:

#!/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