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

发布于 2024-12-04 13:18:38 字数 384 浏览 4 评论 0原文

由于工作原因,我需要卸载并重新安装 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”打印正确的命令。 如果取消注释,第二行将正常工作。

任何人都可以帮忙找出问题所在吗?

For work reason, I need to uninstall and reinstall several apps 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2024-12-11 13:18:38

“失败”...我使用 dos2unix 来解决同样的问题,但像 tr -d "\r" 更好。
并非每个 adb shell 命令都会附加 MSDOS CRLF,但只是某些时候而已。

adb 卸载失败

'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

梦境 2024-12-11 13:18:38
#!/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
#!/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
弥繁 2024-12-11 13:18:38

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文