BIRKEY CONSULTING

ABOUT  GITHUB  RSS  ARCHIVE  DONATE


10 Mar 2020

One command to drive all

Over the years, I got to learn various package managers either due to tinkering with work or home infrastructure. I need to keep remembering if I am on Ubuntu, OSX or on OpenBSD if I want to install/remove/list/update packages. wouldn't it be nice if all I need to remember is just one command pkg with following neumonic options?

Following shell script does exactly that. And it could also serve as your reference to all unix package managers as an added benefit.

#!/bin/sh
# One pkg command to rule them all
if [ -z "$1" ]
then
    echo "Usage: pkg <i(nstall)|r(emove)|s(earch)|u(pdate)|l(ist)>"
else
    os=$([[ -f /etc/os-release ]] && grep ^ID= /etc/os-release | cut -d = -f 2 || echo `uname`)
    echo --------------------- $os --------------------
    command=$1
    shift
    case $os in
	*OpenBSD*)
	    case $command in
		i)
		    pkg_add $@
		    ;;
		r)
		    pkg_delete $@
		    ;;
		s)
		    pkg_info -Q $@
		    ;;
		u)
		    pkg_add -u $@
		    ;;
		l)
		    pkg_info -mz
		    ;;
		c)
		    pkg_check
		    ;;
	    esac
	    ;;
	*Arch)
	    case $command in
		i)
		    yay -S $@
		    ;;
		r)
		    yay -Rs $@
		    ;;
		s)
		    yay -Ss $@
		    ;;
		u)
		    yay -Syyu $@
		    ;;
		l)
		    yay -Q $@
	    esac
	    ;;
	*solus*)

	    case $command in
		i)
		    sudo eopkg it $@
		    ;;
		r)
		    sudo eopkg remove $@
		    ;;
		s)
		    sudo eopkg search $@
		    ;;
		u)
		    sudo eopkg up
		    ;;
		l)
		    sudo eopkg list-installed
	    esac
	    ;;
	VoidLinux)
	    case $command in
		i)
		    sudo xbps-install $@
		    ;;
		r)
		    sudo xbps-remove -R $@
		    ;;
		s)
		    sudo xbps-query -Rs $@
		    ;;
		u)
		    sudo xbps-install -Su
		    ;;
		l)
		    sudo xbps-query -l
		    ;;
		c)
		    sudo xbps-remove -Oo
		    ;;
	    esac
	    ;;
	pureos|PureOS|debian|Ubuntu)
	    case $command in
		i)
		    sudo apt-get install $@
		    ;;
		r)
		    sudo apt-get remove $@
		    ;;
		s)
		    apt-cache search $@
		    ;;
		u)
		    sudo apt-get update && sudo apt-get upgrade
		    ;;
		l)
		    sudo apt list --installed
		    ;;
		c)
		    sudo apt-get autoremove
		    ;;
	    esac
	    ;;
	darwin*|Darwin*)
	    case $command in
		i)
		    brew install $@
		    ;;
		r)
		    brew remove $@
		    ;;
		s)
		    brew search $@
		    ;;
		u)
		    brew update
		    ;;
		l)
		    brew list
		    ;;
		c)
		    brew cleanup
		    ;;
	    esac
	    ;;
	*)
	    echo "Unknown OS. Please add it to $(basename "$0") pkg function"
    esac
fi

If you happen to use Emacs and Eshell, you can just use following alias to get going:

alias pkg ./pkg '$*'

Eshell aliases are great. You can pass arguments to them just prefixing aliases with '$*' as I did above. Try doing same with bash alias, you are out of luck and you have to write shell functions be able to do that. Enjoy!

Tags: shell