【问题标题】:adb uninstall command doesn't work in Bash shell for statement [duplicate]adb 卸载命令在 Bash shell 中对语句不起作用 [重复]
【发布时间】:2011-09-15 05:00:59
【问题描述】:

出于工作原因,我需要在 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' 总是打印“失败”,而 'echo' 打印正确的命令。 如果取消注释,第二行将正常工作。

谁能帮忙找出问题所在?

【问题讨论】:

  • 现在让我自己回答。包名中有“\r”。所以使用tr -d "\r" 删除这个字符就可以了。

标签: android shell adb


【解决方案1】:

'Failed' ...我使用 dos2unix 来解决同样的问题,但像 tr -d "\r" 更好。 不是每个 adb shell 命令都附加 MSDOS CRLF,但只是在某些时候。

adb uninstall failed

【讨论】:

    【解决方案2】:
    #!/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
    

    【讨论】:

      【解决方案3】:

      是必要的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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-27
        • 1970-01-01
        • 1970-01-01
        • 2015-03-25
        • 1970-01-01
        • 2012-10-08
        • 2017-02-06
        • 1970-01-01
        相关资源
        最近更新 更多