#!/bin/bash # Autor: Matias Linares VIEWER=evince PARAMS=("$@") re='^[0-9]+$' function usage { printf "$0 [CHAPTER] [MANPAGE] [FILE]\n\n" printf "Converts a man page to pdf. If the file to save is not defined, it will open\n" printf "with the PDF viewer of your choice.\n\n" printf "[CHAPTER] and [FILE] can be omitted.\n" } function isnumber { num=$1 if ! [[ $num =~ $re ]]; then echo 0 else echo 1 fi } assert_manpage() { man -t $1 $2 > /dev/null || exit 1 } if [ ${#PARAMS[*]} -eq 0 ] || [ ${#PARAMS[*]} -gt 3 ] || [ "${PARAMS[0]}" = "-h" ] || [ "${PARAMS[0]}" = "--help" ] then # Fallan los parametros o queremos ver la ayuda :p usage elif [ ${#PARAMS[*]} -eq 1 ] then # Un solo parametro assert_manpage ${PARAMS[0]} name="/tmp/${PARAMS[0]}.pdf" manpage=${PARAMS[0]} man -t $manpage | ps2pdf - > "$name" $VIEWER $name 2> /dev/null & elif [ ${#PARAMS[*]} -eq 2 ] then # Son dos parametros. if [ $(isnumber ${PARAMS[0]}) -eq 0 ] then # Tenemos un archivo al que escribir assert_manpage ${PARAMS[0]} manpage=${PARAMS[0]} name="${PARAMS[1]}" man -t ${PARAMS[0]} | ps2pdf - > "${PARAMS[1]}" else # Vemos el archivo con su respectiva pagina de manual assert_manpage ${PARAMS[0]} ${PARAMS[1]} name="/tmp/${PARAMS[1]}${PARAMS[0]}.pdf" chapter=${PARAMS[0]} manpage=${PARAMS[1]} man -t $chapter $manpage | ps2pdf - > "$name" $VIEWER $name 2> /dev/null & fi else name="${PARAMS[2]}" chapter=${PARAMS[0]} manpage=${PARAMS[1]} man -t $chapter $manpage | ps2pdf - > "$name" fi exit 0