Thanks to visit codestin.com
Credit goes to jugad2.blogspot.com

Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, March 4, 2017

m, a Unix shell utility to save cleaned-up man pages as text

By Vasudev Ram



mu image attribution

I was using this Unix utility called m today, as I often do, when working on Linux. It's a shell script that lets you save the man pages for one or more Unix commands, system calls or other topics, to text files, after cleaning up the man command output to remove formatting meant for emphasis, printing, etc.

I had first written m a while ago, on Unix boxes that I used to work on earlier, as opposed to Linux, which I use these days.
At that time it was needed, because on some Unix versions, man page output used to be formatted for printers (by text-processing tools such as nroff and troff. These tools would insert extra formatting characters in the output, for effects like bold and underscore, that made it less easy to read the text file on screen, if you simply redirected it to a file, and opened it in a text editor. (Reading the page via the man command itself would work fine.)

Here is the m script, shown by cat [1]:
cat ~/bin/m
cat displays:
mkdir -p ~/man
for i
do
    man $i | col -bx > ~/man/$i.m
done
[1] Check out "useless use of cat" at the cat link above.

(A less-known fact is that "for i" is shorthand for "for i in $*", i.e. it iterates over all the command-line arguments to the script. Not to be confused with "for i in *" which will iterate over all the filenames in the current directory, because the * expands to that.)

m uses the convention of putting all the text files that it creates (one per command-line argument), into a directory called man, under your home directory, i.e. ~/man. If the directory does not exist, it will be created.

You have to save the above script as a file called m in a directory that is in your Unix PATH. Creating a directory called ~/bin is a good choice - your local bin directory:
mkdir ~/bin
cp m ~/bin  # Assumes you created m in your current directory.
and make it executable using chmod:
chmod u+x ~/man/m
Now if I run m as follows, to generate (as cleaned-up text) the man pages for, say, the fopen and fclose C stdio library functions,
m fopen fclose
it creates the text files fopen.m and fclose.m in my ~/man directory.
I can then open fopen.m with the view command (vi in read-only mode):
view ~/man/fopen.m
Here is a screenshot of the file opened in vi(ew):


Enjoy.

P.S. If you are new to vi and want to get up and running with it fast, check out my vi quickstart tutorial. I first wrote it for a couple of friends, Windows system administrator colleagues of mine, who had been given additional charge of a few Unix boxes, at their request, to help them to get up to speed with vi. They later said it helped with that.

P.P.S. If you like short words and commands like m, check out the Japanese word mu for some interesting points.

A few excerpts:
[
The Japanese and Korean term mu (Japanese: 無; Korean: 무) or Chinese wú (traditional Chinese: 無; simplified Chinese: 无) meaning "not have; without" is a key word in Buddhism, especially Zen traditions.
...
Some English translation equivalents of wú or mu 無 are:
"no", "not", "nothing", or "without"[2]
nothing, not, nothingness, un-, is not, has not, not any[3]
[1] Nonexistence; nonbeing; not having; a lack of, without. [2] A negative. [3] Caused to be nonexistent. [4] Impossible; lacking reason or cause. [5] Pure human awareness, prior to experience or knowledge. This meaning is used especially by the Chan school.
...
The character wu 無 originally meant "dance" and was later used as a graphic loan for wu "not". The earliest graphs for 無 pictured a person with outstretched arms holding something (possibly sleeves, tassels, ornaments) and represented the word wu "dance; dancer".
...
The Gateless Gate, which is a 13th-century collection of Chan or Zen kōans, uses the word wu or mu in its title (Wumenguan or Mumonkan 無門關) and first kōan case ("Joshu's Dog" 趙州狗子). Chinese Chan calls the word mu 無 "the gate to enlightenment".[9] The Japanese Rinzai school classifies the Mu Kōan as hosshin 発心 "resolve to attain enlightenment", that is, appropriate for beginners seeking kenshō "to see the Buddha-nature"'.[10]
...
In the original text, the question is used as a conventional beginning to a question-and-answer exchange (mondo). The reference is to the Mahāyāna Mahāparinirvāṇa Sūtra[14] which says for example:
In this light, the undisclosed store of the Tathagata is proclaimed: "All beings have the Buddha-Nature".[15]
...
In Robert M. Pirsig's 1974 novel Zen and the Art of Motorcycle Maintenance, mu is translated as "no thing", saying that it meant "unask the question". He offered the example of a computer circuit using the binary numeral system, in effect using mu to represent high impedance:
...
"Mu" may be used similarly to "N/A" or "not applicable," a term often used to indicate the question cannot be answered because the conditions of the question do not match the reality.
...
Because of this meaning, programming language Perl 6 uses "Mu" for the root of its type hierarchy.[23]
]

The image at the top of the post, is the character mu in seal script.

P.P.P.S. Really the last this time :) Another m-word:

Check out Muji - simplicity is deceptively complex.

Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



Monday, November 4, 2013

ShellCheck.net, useful site for sh/bash scripters

By Vasudev Ram



Saw this via a tweet by Cameron Laird (@Phaseit):

ShellCheck.net is a web site where you can paste in your sh or bash script, and it will statically analyse and lint it.

Excerpt from the ShelllCheck.net site:

[ ShellCheck is a static analysis and linting tool for sh/bash scripts. It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures. ]

Apparently the tool is written in Haskell.

I tried it with the default example shown, and it seems to work, since it gave 5 errors or warnings about the code.

I then tried it again with this simple shell script:
a=1
while $a -lt 5
do
    echo $a
    a=`expr $a + 1`
done

and it found a couple of issues with it: that the backquotes are deprecated, and that expr is antiquated.

- Vasudev Ram - Dancing Bison Enterprises

Contact me



DiscountMags.com



Monday, May 20, 2013

HuffShell suggests aliases for your Unix commands

paulmars/huffshell · GitHub

huffshell is a gem to suggest optimized aliases for your frequently used Unix commands. It looks at your command history to do that.

Seen via this Hacker News post which is interesting too:

What are your top 100 unix commands? (for science) :

https://news.ycombinator.com/item?id=5733426

- Vasudev Ram
dancingbison.com

Friday, November 30, 2012

Clink adds bash-like features to Windows' cmd.exe

clink - Bringing Bash's powerful command line editing to Microsoft Windows' cmd.exe - Google Project Hosting

An interesting feature is that you can add Clink support to an already running cmd instance with its inject option.

- Vasudev Ram
www.dancingbison.com

Wednesday, July 4, 2012

Generate all Linux command man pages in one shot


Just saw this today:

Linux man-pages: list of all pages, by section

Useful. It does not include most commands (i.e. binaries), though. It mainly covers system calls and C library functions. The site says that that is the case because most commands come with their own man pages.

If you want to create your own set of man pages for Linux commands or for Unix commands, you can easily do it with this short shell script:

export MAN_DIR=some_directory_name
for dir in $CMD_DIRS
do
    cd $dir
    for cmd in `ls`
    do
        man $cmd >$MAN_DIR/$cmd.m
    done
done

where CMD_DIRS has a list such as:

"/bin /usr/bin /etc /sbin /usr/sbin" in it.

That's typically one of the first things that I used to do when using a new Unix system :)

And then you can use:

vi *.m

to read them all  ...

Or use * instead of ls in backquotes.
Posted via mobile, sorry for any formatting issues.

- Vasudev Ram
www.dancingbison.com

Monday, March 26, 2012

UNIX means YOU get to choose your tools (*)

http://newcome.wordpress.com/2012/03/06/functional-programming-and-the-death-of-the-unix-way/

See the comment by Jmp on the above post.

(*) And if no tool is _just_ right for the job at hand, UNIX facilitates writing your own (in C, sh, sed/awk/et al, or in Perl, Python, Ruby, or other languages, with great integration and interaction with the OS, the shell and the many powerful command-line tools that are an integral part of UNIX.

Want proof? Just read the book "The UNIX Programming Environment" by Kernighan and Pike, and try out many of the examples in it.

Sunday, February 5, 2012

Good list of Linux command-line resources


http://linuxcommand.org/who_what_where_why.php

See some way down that page.

- Vasudev Ram
www.dancingbison.com