#! /bin/bash # justify text / usage: just # option check and set formatter while getopts :r:l:c: opt do case $opt in r) flg=$opt; n=$OPTARG; fmt="printf \"%${n}s\\n\"" ;; l) flg=$opt; n=$OPTARG; fmt="printf \"%-${n}s\\n\"";; c) flg=$opt; n=$OPTARG;; *) echo "usage: $0 [-rcl WIDTH]"; exit 1;; esac done # print stdin with format while read buf do if [[ $flg = 'c' ]]; then let n1=n/2 let n2=${#buf}/2 let n3=n1+n2 fmt="printf \"%${n3}s\\n\"" fi eval "$fmt \"$buf\"" done