linux shell output to html

后端 未结 5 1829
时光说笑
时光说笑 2020-12-08 05:45

is there any way to convert bash output to html ? for example if I had some colorized output in bash ( something like htop ), how can I convert it to html tags ... ( somethi

5条回答
  •  一向
    一向 (楼主)
    2020-12-08 06:20

    As suggested in @eli's answer, you can use script to capture the colored output to a file.

    You can then convert the output to HTML with aha, the "Ansi HTML Adapter", which should be available in your distribution's repositories: apt install aha or yum install aha for Debian-based or Redhat-based distributions.

    If you want to capture only a single command, use the -c option:

    script -c "grep --color ..."
    

    This will save the output to a file named typescript in your current directory.

    To save to a different file, add the file name at the end:

    script -c "grep --color ..." my_grep_colored_output
    

    See man script for other options.

    To capture several commands from an interactive session, just start script without a command, and enter Ctrl-d to exit script when done.

    To just view the file in color, use less -R.

    To convert it to HTML with aha :

    aha -s -f typescript > output.html
    

    or

    aha -s < typescript > output.html
    

    The -s option makes it write a style sheet in the html header instead of using inline styles through the file. That makes it easier to change colors if some background/foreground combinations turn out hard to read in a browser.

提交回复
热议问题