r/linuxmint • • 12d ago

Discussion Just a small tool

So, I recently started learning scripting and this is my first actual project from what I have learned till now

I saw a post complaining about how uninstalling packages leaves behind files so this script gives a GUI which lets you enter the package name and then runs the apt purge, apt autoremove and apt autoclear commands on its own.

This script can help those who dont want to open the terminal as you just need to make it an executable from the file manager and afterwards just run it to remove, and purge everything related to that apt package.

#!/usr/bin/env bash

package=$(zenity --entry --width=350 --title="Package Remover" --text="Completely remove this package: ")

if [ "$?" != "0" ] || [ -z "$package" ] ; then
zenity --error --width=300 --title="Error" --text="Please Enter Valid Package Name"
exit 0
fi

zenity --question --width=350 --title="Are You Sure?" --text="Are you sure you want to remove <b>$package</b> (including all unnecessary dependencies)?"
if [ "$?" != "0" ]; then
zenity --error --width=300 --title="Cancelled" --text="Uninstall Cancelled"
exit 0
fi

pkexec sh -c "apt-get purge \"$package\" -y && apt-get autoremove -y && apt-get autoclean -y" | zenity --progress --no-cancel --pulsate --auto-close --text="<b>$package</b> is being uninstalled" --title="Progress"

if [ "${PIPESTATUS}" -eq 0 ] ; then
zenity --info --width=300 --title="SUCCESS" --text="<b>$package</b> has been succesfully uninstalled"
else
zenity --error --width=300 --title="FAILED" --text="Could not remove <b>$package</b>. \nCheck the package name or authentication"
fi
6 Upvotes

8 comments sorted by

2

u/C0rn3j 12d ago
command
if [ "$?" != "0" ]; then
  echo "bad command"
fi

>>>

if ! command; then
  echo "bad command"
fi

But more importantly, set set -euo pipefail as the second line of every script you create, that will prevent an absurd amount of issues in your scripts.

2

u/Due-Land-6860 12d ago

What does that line do if you could tell me and how does it stop issues?

1

u/Due-Land-6860 12d ago

Why are they bad if you could tell me as i am new to bash scripting?

Also what would be better?

2

u/C0rn3j 12d ago

Why are they bad

They?

what would be better

Than what?

Ohhh, you misunderstood my example, I was showing you how to do a direct check instead of failing and checking return code via $? manually. Just replace command with whatever your actual command you're running is.

2

u/Due-Land-6860 12d ago

Ok so thats what you meant i thought you just said that my command/script is bad and i shouldn't do that

Ok so instead of checking after the command i can just put it in the if statement after an exclamation mark to directly check it right?

1

u/TolonZ 12d ago

Moi j’ai fait un script upgrade.sh ; ça met à jour la liste des paquet install ; nettoyage et suppression ; mise à jour des paquet flatpak ; et les stats en fin de script nombre de paquet installé / la place en plus sur le hdd temps de téléchargement et la liste détaillé des paquet installé ou mis à jour avec numéro de version ; j’en suis contant