Monday, November 5, 2012

ManToFile: Export MAN Pages to Text File

I've been busy learning some basic scripting in Bash lately. It's not for a serious purpose or something, I just want to be able to do some personal task in automated way using script, so I don't have to do repetitive steps manually everytime.

Bash shell is great tool to do the job, and it provides many useful, straight forward, built-in commands and utilities. We just have to know and learn how to use them.

Using Google and other websites, you can find many useful informations about bash scripting on the internet. It gave me answers to most of my questions in using Bash. But still, in some point or situation, I just want to know the basic, built-in command and utilities in Bash, using official manual how to use them (MAN pages). Combination of reading official MAN pages and working example use of commands gave me better understanding to create my own codes.



With my basic knowledge in Bash scripting, I made a simple, yet very useful (at least for me) script, I gave it a name: ManToFile. As you can suggest, it's a script to redirect the "man" command output to text file, so you can read the MAN page not only in a terminal window, but in a text editor application. Why did I make such silly script? Well, for me, it's more convenience and flexibel to read the text of MAN pages in a text editor window instead of in Terminal.

For those who interested, you can download here:


mantofile_2012.11.06.tar.gz

ManToFile is free/open source (GNU GPL v.3 licensed)

Extract the downloaded file. To use the script, open Terminal window, navigate to the script location, and simply enter command mantofile or mantofile [MANPAGE], where [MANPAGE] is MAN page name that you want to read.

Then, the script will ask you to specify directory for output file, or you can simply press [ENTER] key to use default one. When succesfully created, the script will offer you to open it in a text editor for your convinience.

If you don't want to download, you can simply copy-paste the following code (nothing special, but it does the job) into a blank text file and then save it as whatever name you want. Mark the script as executable, and you are ready to use it:

#!/bin/bash

# Copyright 2012, Zon Saja <zonsaja@gmail.com> - http://fandigital.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

clear
echo "`basename $0` - Simple tool to export MAN pages to file"
echo "Usage: `basename $0` [MANPAGE]"
echo "-----------------------------------------------------------------------------"
echo

OutputDirDefault="$HOME/man";
ManFileExt="man";
ManPage=;
OutputDir=;

OpenManFilePrompt () {
 echo "$OutputDir/$ManPage.$ManFileExt created."; echo;
 read -n1 -r -p "Would you like to open it in a text editor?" InputKey;
 if [ $? -eq 0 ]; then
  if [ -z "$InputKey" ] || [ "$InputKey" = "y" ]; then
   nohup xdg-open "$OutputDir/$ManPage.$ManFileExt" > /dev/null 2>&1;
  fi;
 fi;
}

CreateManFile () {
 if $(man $ManPage > /dev/null 2>&1); then
  echo "Type output directory (default is $OutputDirDefault) and press [ENTER]:";
  read OutputDir;
  [ -z "$OutputDir" ] && OutputDir="$OutputDirDefault"
  (mkdir -p "$OutputDir" && man $ManPage | col -bx > "$OutputDir/$ManPage.$ManFileExt" && OpenManFilePrompt) || (echo ; echo "File export failed. Is '$OutputDir' writeable?");
 else
  echo "Sorry, no manual entry for '$ManPage'";
 fi;
}

CreateManFilePrompt () {
 echo "Type a MAN page and press [ENTER]:";
 read ManPage;
 if [ ! -z "$ManPage" ]; then
  CreateManFile;
 else
  echo "No MAN page specified.";
 fi;
}

if [ $# -eq 1 ]; then
 ManPage="$1";
 CreateManFile;
else
 CreateManFilePrompt;
fi;

echo;

For your convenience, I suggest you to put the script in bin folder within your home directory (/home/[YourUserName]/bin*). so that you can use it from any working directory in Terminal, without having to navigate (cd) to the script location. This way, you can simply call it by typing its name without dot and trailing slash in front of command (just type mantofile command instead of ./mantofile).

*) This is the location of user's bin Bash' PATH environment variable which is preconfigured in Ubuntu. It may differ in other Linux distros. Or you can modify /create .bashrc file located in your home folder to specify another location for Bash' PATH variable.

.


PS: Still, sorry for my English :)

No comments:

Post a Comment

Google+ Comments Disqus Comments
Disqus Comments Google+ Comments
comments powered by Disqus