BIRKEY CONSULTING

ABOUT  RSS  ARCHIVE


Posts tagged "shell":

27 Mar 2021

less is literally more

As an Emacs user, your muscle memories constantly remind you of certain behavior of it when you had to use other Unix tools. I was recently viewing a large file using less and searched for ErrorCode by just typing errorcode and surprised to find that it did not find any match. Then, I searched by typing ErrorCode and it did find a match containing the word, which reminded me of the fact that less by default is case sensitive. I then set out to find if I can tell less to ignore case by reading man pages. I am pleasantly surprised to read following:

-i or --ignore-case
	      Causes searches to ignore case; that is, uppercase and lowercase
	      are  considered identical.  This option is ignored if any upper-
	      case letters appear in the search pattern; in other words, if  a
	      pattern  contains  uppercase  letters, then that search does not
	      ignore case.

That is exactly how Emacs isearch behaves by default. I added following alias to my bash config to make above the default for less.

alias less='less -i'

Once again, less is literally more and you could do more using less.

Tags: shell
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
Other posts