Generate documentation from Shell output

喜欢而已 提交于 2019-12-24 03:27:56

问题


Is there a way/tool to generate HTML documentation (similar to what doxygen does) directly from my Shell output or even a saved log ? If nothing is available, do you guys have any creative ideas on how to do that with the existing tools ?

I'm thinking that while typing, I can put some sort of mark or special character and then have a tool pick that up as the beginning of a comment, while the rest of what is typed are CLI and output.

Example :

ubuntu@nso-172-73:~$ # This is a comment
ubuntu@nso-172-73:~$ ping google.com
PING google.com (172.217.9.174) 56(84) bytes of data.
^C
--- google.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1008ms

ubuntu@nso-172-73:~$ # End of Comment

回答1:


This doesn't quite give a complete answer to your question, but it will hopefully give you a start.

bash has a script command. That's a confusing enough name, eh? Basically, script is an interactive terminal logger. Since there's no whatis entry for it, here's the start of the man page:

$ man script | head -n 17 | sed '/^$/d'
SCRIPT(1)                        User Commands                       SCRIPT(1)
NAME
       script - make typescript of terminal session
SYNOPSIS
       script [options] [file]
DESCRIPTION
       script makes a typescript of everything displayed on your terminal.  It
       is useful for students who need a hardcopy  record  of  an  interactive
       session  as  proof  of  an  assignment,  as  the typescript file can be
       printed out later with lpr(1).
       If the argument file is given, script saves the dialogue in this  file.
       If no filename is given, the dialogue is saved in the file typescript.

Let me give you an example, then I'll show you details of what I do with this command and give an idea of how this could be used to accompl. I'm running this from my Cygwin terminal.

$ script
Script started, file is typescript

$ pwd
/home/me

$ # This is a comment

$ ping google.com
PING google.com (172.217.12.206): 56 data bytes
64 bytes from 172.217.12.206: icmp_seq=0 ttl=51 time=68 ms
64 bytes from 172.217.12.206: icmp_seq=1 ttl=51 time=67 ms
64 bytes from 172.217.12.206: icmp_seq=2 ttl=51 time=67 ms
64 bytes from 172.217.12.206: icmp_seq=3 ttl=51 time=67 ms
64 bytes from 172.217.12.206: icmp_seq=4 ttl=51 time=68 ms
64 bytes from 172.217.12.206: icmp_seq=5 ttl=51 time=67 ms

----google.com PING Statistics----
6 packets transmitted, 6 packets received, 0.0% packet loss
round-trip (ms)  min/avg/max/med = 67/67/68/67

$ # End of Comment

$ cd /

$ pwd
/

$ exit
exit
Script done, file is typescript

$ pwd
/home/me

Now we'll look at the resultant file. Don't try head or cat, because you won't see the problem (or feature, if you want the `^C') we encounter.

Here are the first few lines from vim typescript

You'll see the escape and control sequences. Those can be removed (see the Clean Up Output section below.)

Now, you can set up a doxygen filter, somewhat like the one described here

(Here it is, since links can die)

Doxygen doesn't support bash script files. While that probably makes sense, to not support them (define a bash script file in terms of functions and variables), there are always a few laying around in a source tree that should go into the document.

It's easiest to add the script files without changing them, so a doxygen input filter. Input filters work pretty good with command line sed or awk commands, so we'll do a simple one here that lets you add Doxygen commands comeent lines starting with ##.

Edit these lines in your Doxyfile:

FILE_PATTERNS = *.sh *.awk

INPUT_FILTER = "sed -e 's|##|//!|'"

FILTER_SOURCE_FILES = YES

Add a brief description to the top of each file:

## functions for OpenEmbedded, and Jenkins -*- shell-script -*-.

## @author rickfoosusa

## @file

Generally anything with # for a comment will work, but for languages you get a lot more documentation by using a filter like asm4doxy.pl http://rudy.mif.pg.gda.pl/~bogdro/inne/asm4doxy.txt

Hopefully, this points you in the right direction. I hope to get back and flesh it out a bit.

Clean Up Output

I use the following two files to get rid of the escape sequences, a perl script

#!/usr/bin/perl
#
##############################################################################
# Takes escape and control sequences out of the `script` command's output
#
# @file output_scrubbed_script.pl

#
# Okay, I found this all over the internet, it's just my favorite.
# This removes all unwanted junk (control characters, etc) from the output
# of the Linux script command.
# It's imperfect. It struggles with vim, and it doesn't do too well with up
# arrows and tab completion, but I can usually at least see and understand
# what I did.
#
##############################################################################

while (<>) 
{
    s/ \e[ #%()*+\-.\/]. |
       \r | # Remove extra carriage returns also
       (?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
       (?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
       (?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
       \e.|[\x80-\x9f] //xg;
       1 while s/[^\b][\b]//g; #remove all non-backspace followed by backspace
    print;
}

and a bash script

#!/bin/bash
#
##############################################################################
# Runs the clean-up
#
# @file script_scrubber.sh
# @author bballdave025
#
# See the usage variable
#
##############################################################################

usage="Usage is:\n > script_scrubber.sh <unscrubbed-file> "\
"[<alternate-filename>]\n"\
"If no <alternate-filename> argument is given, the <unscrubbed-file> \n"\
"will be cleaned in place.\n\n"\
"This script takes the output of the Linux script command and cleans"\
"control characters and other unwanted artifacts."\
"It uses output_scrubbed_text.pl\n"

scrubber_path="$HOME"
scrubber_location="${scrubber_path}/output_scrubbed_text.pl"

if [[ ! -f "${scrubber_location}" ]]; then
  echo -e "Fatal error! ${scrubber_location} does not exist."
  exit 1

fi #endof:  if [[ -f "${location}/output_scrubbed_text.pl" ]]

if [[ ! -x "${scrubber_location}" ]]; then
  chmod +x "${scrubber_location}"
fi #endof:  if [[ ! -x "${scrubber_location}" ]]

if [[ $# -eq 0 ]]; then 
  echo "No argument given."
  echo -e "${usage}" 
  exit 2

elif [[ $# -eq 1 ]]; then
  if [[ $1 == "--help" ]]; then
    echo -e "${usage}"
    exit 0
  else
    "${scrubber_location}" $1 > tmp && mv tmp $1
  fi #endof:  if [[ $1 -eq "--help" ]]

elif [[ $# -eq 2 ]]; then
  "${scrubber_location}" $1 > $2

else
  echo "More than two arguments given."
  echo -e "${usage}"
fi #endof:  if [[ $# -eq <0,1,2,else> ]


来源:https://stackoverflow.com/questions/43012530/generate-documentation-from-shell-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!