One Minute Tips and Tricks

Quickie Command Line Parser with Make

Occasionally we may need to create a wrapper script or similar utility script that a user can run. We can do this in Bash or Python easily enough, but sometimes even those are overkill. Luckily, there's the make utility which can accept command line arguments and other parameters.

#!/usr/bin/make -f


@echo "This is some helpful text."


@sudo systemctl start myapp


@echo "This configures the application."

The advantage is that there are no bashopts or pyargs library to configure, the code is eminently readable, and it's available on most systems.

Bash up function

I add this to my .bashrc to quickly escape from a nested directory structure.

function up(){ unset upvar1; for item in $(seq 1 $1); do upvar1=${upvar1}"../"; done; echo cd $upvar1; cd $upvar1;}

For example:

[kwan@rocinante tasks]$ pwd


[kwan@rocinante tasks]$ up 3

cd ../../../

[kwan@rocinante playbooks]$ pwd


Auto-Alias Python Virtual Environments

I use this snippet in my bash startup scripts to automatically alias the Python virtual environments. This makes it easy to start a virtualenv.

for item in ~/bin/python_venv/*; do penv=$(basename $item); alias $penv="source $item/bin/activate"; done

In my ~/bin/python_venv/ path I create all my Python virtual environments. Once I login or re-rerun bash, all the environments can be executed as an alias.

(p36) [kwan@infinity python_venv]$ python -m virtualenv -p python3.6m p36m

Running virtualenv with interpreter /home/kwan/bin/anaconda/bin/python3.6m

Already using interpreter /home/kwan/bin/anaconda/bin/python3.6m

Using base prefix '/home/kwan/bin/anaconda'

New python executable in /home/kwan/bin/python_venv/p36m/bin/python3.6m

Also creating executable in /home/kwan/bin/python_venv/p36m/bin/python

Installing setuptools, pip, wheel...


(p36) [kwan@infinity python_venv]$ ls

p27 p36 p36m

(p36) [kwan@infinity python_venv]$ alias

alias p27='source ~/bin/python_venv/p27/bin/activate'

alias p36='source ~/bin/python_venv/p36/bin/activate'

(p36) [kwan@infinity python_venv]$ bash --login

[kwan@infinity python_venv]$ alias

alias p27='source /home/kwan/bin/python_venv/p27/bin/activate'

alias p36='source /home/kwan/bin/python_venv/p36/bin/activate'

alias p36m='source /home/kwan/bin/python_venv/p36m/bin/activate'