Getting Hands-on With Pacman

Though Manjaro has a graphic utility for adding/removing software, it does ship with pacman which is the the package manager from upstream Arch Linux. On Ubuntu based systems, I was already familiar with apt-get command with its straight forward options, but pacman was little different in terms of the flags it uses to install/update packages. It would take me some time before getting used to it. And while I was still getting a hang of it I knew I would be making ample mistakes on the cli and probably mess something up. There was definitely a better way to handle this elegantly and an opportunity to use my nifty Unix scripting skills.

The official Pacman wiki is detailed enough to understand and use the different options. To use this seamlessly I thought it would be good to write a Unix script as a wrapper over these commands, which will make sure that I just provide the choice of my actions and name of the package for it to work. The script will take care of calling the right commands using sudo ( if required ). This takes away the need to figure out which flags to pass to pacman.

I added options to search for packages, install/remove package, list/remove redundant packages, upgrade system, clear cache etc. Selected menu item simply delegates to the appropriate pacman command, making it a stress-free, seamless execution. Here is the entire script.

#! /usr/bin/sh

while [ true ]
do
        clear
        echo "PACMAN Helper"
        echo "-------------"
        echo "1. Search package"
        echo "2. Install package"
	echo "3. Remove package"
        echo "4. List unused packages"
        echo "5. Remove unused packages"
	echo "6. Update and Upgrade system"
	echo "7. Clean cache"
        echo "0. Exit"
        echo
        read -p "Enter choice: " choice
		echo 

        case $choice in
                0)      clear
			exit
                        ;;
                1)      read -p "Enter package name: " pkg
			(pacman -Qs $pkg; pacman -Ss $pkg) | cat | less
                        ;;
                2)      read -p "Enter package name: " pkg
                        sudo pacman -Syu $pkg
                        ;;
                3)      read -p "Enter package name: " pkg
                        sudo pacman -R $pkg
			read -p "Press any key to continue..." continue
                        ;;
		4)	if [ `pacman -Qdt | wc -l` -gt 0 ];then
				echo -e "Listing unused packages...\n"
				pacman -Qdt
			else
				echo "No unused packages found"
			fi
			read -p "Press any key to continue..." continue
			;;
		5)	if [ `pacman -Qdt | wc -l` -gt 0 ];then
				sudo pacman -Rs $(pacman -Qdtq)
			else
				echo "No unused packages found"
			fi
			read -p "Press any key to continue..." continue
			;;
		6)	sudo pacman -Syu
			read -p "Press any key to continue..." continue
			;;
		7) 	sudo pacman -Sc
			read -p "Press any key to continue..." continue
			;;
                *)      echo "Invalid choice"
                        ;;
        esac
done

Created a ~/.local/bin folder and saved this file as “pacmanhelper”. File permission 744 makes sure no one else is able to change or execute this file. I updated the .bashrc to include this in the PATH.

Here are some screenshots of the script in action.

Leave a comment