Created
January 26, 2019 13:01
-
-
Save h8rt3rmin8r/aec12da9c24e15f035ec9053cab4dc24 to your computer and use it in GitHub Desktop.
Translate Unix manual pages into text files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
#============================================================================# | |
# ________ __ __ | |
# _____ _____ ____ \_____ \_/ |____ ____/ |_ | |
# / \\__ \ / \ / ____/\ __\ \/ /\ __\ | |
# | Y Y \/ __ \| | \/ \ | | > < | | | |
# |__|_| (____ /___| /\_______ \|__| /__/\_ \ |__| | |
# \/ \/ \/ \/ \/ | |
# | |
#============================================================================# | |
# | |
# INFORMATION | |
# | |
# man2txt - Translate Unix manual pages into text files | |
# | |
# Created by h8rt3rmin8r on 20190126 | |
# Source: http://bit.ly/man2txt-sh | |
# | |
# USAGE | |
# | |
# ./man2txt.sh <UNIX_COMMAND> | |
# ./man2txt.sh <COMMAND_1> <COMMAND_2> ... <COMMAND_N> | |
# ./man2txt.sh [OPTIONS] | |
# ./man2txt.sh [OPTIONS] <COMMAND> | |
# ./man2txt.sh [OPTIONS] <COMMAND_1> <COMMAND_2> ... <COMMAND_N> | |
# | |
# OPTIONS | |
# | |
# ______________________________________________________________________ | |
# | | | | |
# | -h,--help | Print the man2txt help text | | |
# | -l,--list | Output a list of completed manual text files | | |
# | -t,--table | Output a table of completed manual text files | | |
# | -c,--clean | Remove empty files from the "~/man" directory | | |
# | -s,--silent | Run silently (requires <COMMAND>) | | |
# | --test | Print all core variables used in man2txt | | |
# |_______________|_____________________________________________________| | |
# | |
#============================================================================# | |
# VARIABLE DECLARATIONS | |
USER=$(cd; pwd | cut -c 7-) | |
HOME="/home/${USER}" | |
SELF="[man2txt]" | |
DT=$(date '+%Y%m%d') | |
DT_NOW="${DT}_$(date '+%H-%M-%S')" | |
VBX="/dev/stdout" | |
HOME_DIR="${HOME}/man" | |
LOGS_DIR="${HOME_DIR}/logs" | |
LOG_FILE="${LOGS_DIR}/${DT}.log" | |
LINE_X='=======' | |
LINE_Y="${LINE_X}${LINE_X}${LINE_X}${LINE_X}" | |
LINE="${LINE_Y}${LINE_Y}" | |
LINE__X='-------' | |
LINE__Y="${LINE__X}${LINE__X}${LINE__X}${LINE__X}" | |
LINEA="${LINE__Y}${LINE__Y}" | |
ALL_INPUTS=() | |
#============================================================================# | |
# FUNCTION DECLARATIONS | |
function help_text() { | |
# Output the script help text | |
cat $0 | head -n 38 | tail -n +2 | sed 's/#/ /g' | |
return | |
} | |
function manual_clean() { | |
# Remove all manual text files that contain zero bytes of data | |
echo "${SELF} Performing integrity checks on output files" &>${VBX} | |
MAN_LIST=( $(ls ${HOME_DIR}) ) | |
for i in "${MAN_LIST[@]}"; | |
do | |
if [[ -f "${HOME_DIR}/$i" && "$(cat ${HOME_DIR}/$i | wc -c)" == 0 ]]; | |
then | |
rm "${HOME_DIR}/$i" | |
fi | |
done | |
echo "${SELF} Done. Output files and logs are located at ~/man" &>${VBX} | |
return | |
} | |
function manual_list() { | |
# Output a list of completed manual text files | |
MAN_LIST=( $(ls ${HOME_DIR}) ) | |
for i in ${MAN_LIST[@]}; | |
do | |
if [[ -f "$i" ]]; | |
then | |
echo "$i" | |
fi | |
done | |
return | |
} | |
function manual_table() { | |
# Output a table of completed manual text files | |
MANUAL_ARRAY=( $(manual_list) ) | |
for i in "${MANUAL_ARRAY[@]}"; | |
do | |
printf '%s\t%s\n' "$i@($(cat $i | wc -c))" | |
done >> temp-data.txt | |
column -t -s@ temp-data.txt | |
rm temp-data.txt | |
return | |
} | |
function run_man2txt() { | |
# Execute the core manual conversion process | |
echo "${SELF} Beginning manual conversion process" &>${VBX} | |
for i in "${ALL_INPUTS[@]}"; | |
do | |
man $i 2>>${LOG_FILE} | col -bx 1> "${HOME_DIR}/$i.m" 2>>"${LOG_FILE}" | |
done | |
echo "${SELF} Done. Output files and logs are located at ~/man" &>${VBX} | |
return | |
} | |
function test_man2txt() { | |
# Testing function for the man2txt script | |
echo "${LINE}" | |
echo "USER: ${USER}" | |
echo "DT: ${DT}" | |
echo "DT_NOW: ${DT_NOW}" | |
echo "VBX: ${VBX}" | |
echo "HOME: ${HOME}" | |
echo "HOME_DIR: ${HOME_DIR}" | |
echo "LOGS_DIR: ${LOGS_DIR}" | |
echo "LOG_FILE: ${LOG_FILE}" | |
echo "${LINEA}" | |
echo "ALL_INPUTS: ${ALL_INPUTS[@]}" | |
echo "${LINE}" | |
return | |
} | |
#============================================================================# | |
# INPUT FILTERING AND OPERATIONS EXECUTION | |
# Filter null inputs | |
if [[ "x$1" == "x" ]]; then help_text; exit 1; fi | |
# Intelligently assign the input data array | |
if [[ "$(echo $1 | cut -c 1)" == '-' ]]; | |
then | |
ALL_INPUTS=( $(echo "${@:2}") ) | |
else | |
ALL_INPUTS=( $(echo "$@") ) | |
fi | |
# Filter out unique requests which don't require a full runtime | |
case "$1" in | |
'-h'|'-help'|'--help') help_text; exit 0 ;; | |
'-l'|'-list'|'--list') manual_list; exit 0 ;; | |
'-t'|'-table'|'--table') manual_table; exit 0 ;; | |
'-c'|'-clean'|'--clean') manual_clean; exit 0 ;; | |
'-test'|'--test') test_man2txt; exit 0 ;; | |
'-s'|'-silent'|'--silent') VBX="/dev/null" ;; | |
esac | |
# Create the required directories if they don't exist | |
mkdir -p "${HOME_DIR}" &>/dev/null | |
mkdir -p "${LOGS_DIR}" &>/dev/null | |
# Create the log file if it doesn't exist and log the current runtime | |
touch "${LOG_FILE}" &>/dev/null | |
echo "${SELF} [${DT_NOW}] ${LINE}" >> ${LOG_FILE} | |
# Execute the core script operations | |
#test_man2txt | |
run_man2txt | |
manual_clean | |
#============================================================================# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment