自学基础1_linux_1_man命令详解

(一)知其然

演示

在linux/unix上想看一下ls的帮助文档,输入下面的命令:

$ man ls

当然也可以看man自身的帮助文档

$ man man

 从输出结果可以看出,man命令会分页输出要查看的命令的名称、格式、描述以及具体参数讲解等等信息。 

除了系统自带的命令,还可以查看第三方软件的操作手册,如安装了分布式版本控制程序git后,输入:

$ man git

也能够将git的格式、各个参数的含义和用法等信息全部显示出来。

更多man的用法请上网查阅,或者阅读$ man man提供的帮助手册

作用

从上面几个例子能够看出,但凡遇到新命令,都可以用man查看它的帮助手册,实属自学一大利器。

man是manual(手册)的简写,几乎所有的linux/unix上的命令行程序都自带手册页,所以但凡遇到想了解的命令或第三方程序,找man就行了.


(二)知其所以然

看过上面的例子,算是过了hello world这一关,遇到任何命令,基本都能拿到一手帮助手册。但这份帮助手册能否看懂,又是另外一项基本功,后面另写文章讲解。


问1:为什么我不知道命令怎么用,但是man却知道?

答:因为man有几乎所有程序的一种叫作man page的手册页


问2:man page操作手册从哪儿来?

答:由各命令行程序的开发者编写


问3:程序员编写的man page是怎么让man自动识别的?

答:当然是将man page按照man的要求放到指定的文件夹,输入man <command>时(<command>为需要查看的具体命令),man会扫描指定的文件夹列表按照默认顺序查找


问4:怎么找到man的配置文件呢?

答:执行man man,查看man的操作手册,发现加-d参数,能够打印debug日志

$ man man
<p class="p1">       <strong>-d</strong>     Don't actually display the man  pages,  but  do  print  gobs  of</p><p class="p1">              debugging information.</p>
查看配置文件路径,可以随便查看一个命令的手册,第一行日志就是配置文件路径:

$ man -d ls
<p class="p1">Reading config file /private/etc/man.conf</p>


问5:知道man的配置信息后,怎样让自己写的一个man page(如foo.1)生效,即输入man foo,能够看到操作手册的信息?

答:查看man.conf文件,便能够发现man扫描man page的路径:

MANPATH_MAP     /bin                    /usr/share/man
MANPATH_MAP     /sbin                   /usr/share/man
MANPATH_MAP     /usr/bin                <span style="color: rgb(255, 0, 0);">/usr/share/man</span>
MANPATH_MAP     /usr/sbin               /usr/share/man
MANPATH_MAP     /usr/local/bin          <span style="color:#ff0000;">/usr/local/share/man</span>
MANPATH_MAP     /usr/local/sbin         /usr/local/share/man
MANPATH_MAP     /usr/X11/bin            /usr/X11/man
MANPATH_MAP     /usr/bin/X11            /usr/X11/man
MANPATH_MAP     /usr/bin/mh             /usr/share/man

/usr/share/man是系统自带程序或一些第三方安装程序放man page的文件夹

文件结构:

$ ls
man1	man3	man4	man5	man6	man7	man8	man9	mann	whatis

man的区段结构(常用的是这8个,不一定都要有,也还有很多特殊用途的区段):

区段 说明
man1 一般命令
man2 系统调用
man3 函数,涵盖C标准函数库
man4 特殊文件(通常是/dev中的设备)和驱动程序
man5 文件格式和约定
man6 游戏屏保
man7 杂项
man8 系统管理命令守护进程

/usr/local/share/man为用户自己的man page存放路径,若文件夹不存在,自己新建。创建好man文件夹后,事情还没完,man是按照区段(sections)去管理man page的,所以创好man目录后,还应该在下面创建子目录,至少要创建一个man1子文件夹,然后将man page放入其中,man就能够识别自定义的帮助手册了。

$ pwd
/usr/local/share/man/man1
$ ls
foo.1

示例:自定义man page文件foo.1,其中后缀.1仅用来标示放在man1中(linux/unix上的文件是按内容归类,不同于windows按后缀管理,在linux上后缀可以随便写,也可以不写,比如bin下面的命令就基本没有后缀),在放入/usr/local/share/man/man1前,执行命令:

$ man foo
No manual entry for foo

放入后执行命令,则能正常展示出手册内容:

$ man foo
foo(1)                                                                  foo(1)

NAME
       foo - my own text editor

SYNOPSIS
       [-d] [-e] [-f filename ]


问6:这么多的开发者写出来的操作手册,man如何做到都能解读?

答:因为有编写规范,默认按照groff格式编写,这里需要了解一下的概念,

什么是宏:一般说来,宏是一种规则或模式,或称语法替换 ,用于说明某一特定输入(通常是字符串)如何根据预定义的规则转换成对应的输出(通常也是字符串)

以foo.1为例:

.TH foo 1 
.SH NAME
foo - my own text editor
.SH SYNOPSIS
.B [-d] [-e] [-f
.I filename
.B ]

其中的.TH .SH .B .I都是宏字符串,解析的时候,会自动将其替换成对应的格式进行处理。

处理后输出的效果如下:

foo(1)                                                                  foo(1)

<strong>NAME</strong>
       foo - my own text editor

<strong>SYNOPSIS
       [-d] [-e] [-f</strong> <em><u>filename</u></em> <strong>]</strong>
关于groff的详细格式,可以通过man查找groff的相关手册,然后根据描述找到需要的手册细看:

$ man -f groff
<pre name="code" class="plain">......
groff_man(7) - groff `man' macros to support generation of man pagesgroff_mdoc(7) - reference for groff's mdoc implementation

 
<pre name="code" class="plain">......
 

然后直接到man7下面查看相关手册,里面有详细的格式描述:

$ man 7 groff_mdoc

问7:难道开发者写的man page手册只能是man才能解读吗?

答:当然不是,man本身是不会解析man page的,而是是调用groff这个文本格式化工具解析man page,然后用less命令分页输出。

使用下面两个语句,看到的输出结果是相同的:

$ groff <span style="color:#ff0000;">-man</span> -Tascii ./foo.1 | less
$ man ./foo.1

而且groff不只输出man格式,还有很多格式可选,详情请man groff,下面便是输出成html格式:

$ groff -www -Tascii ./foo.1 | less


问8:man page的内容有固定要求吗?

答:man page基本都会有几项按惯例或规范必须具备的内容,最好遵守,也可以自定义内容

NAME
This is the name of the program, followed by a short (read: short) description of what it does or what the name stands for if it is in an acronym. This will be read by apropos, man -k, makewhatis and whatis
SYNOPSIS
Basically, this is the syntax used to run your program from the command line. An example would be like: foo [-d] [-e] [ -f filename]

DESCRIPTION
This is where you describe what the program does. Because this is what the user is most likely going to look at first, it is important that this section is clear. If the user does not understand what is written here, you can expect your inbox to be filled with emails.

OPTIONS
If your program uses options, as in the above SYNOPSIS section, this is where you explain what each option does. For instance, what does [-d] do when it is given as an argument to program foo? Explain here.

BUGS
If there are any bugs in your program, or anything that does not work the way you want it to, put it here so the user knows.

AUTHOR
Your name followed by your email address. Your email address is important here for a couple of reasons. Firstly, people can give you bug reports so you can fix up your program. Secondly, if you get stupid emails, you can reply with RTFM.

SEE ALSO
If your program is related in a way to another program, specify the program's name here, followed by its section number. For instance, program foo is a text editor. You might want to make references to say, the vi text editor as: vi(1) 

(三)为我所用

1、能够通过man学习了解别人写得程序

2、能够通过man让别人学习了解自己写得程序


总结:

要让man <command>能够输出操作手册的内容,需要如下过程:

1、开发者按groff规范写一份文件

2、将其放到man配置的man page存放路径

3、输入man <command>时,man通过配置路径找到command的man page文件

4、调用groff将其格式化输出成man要展示的内容,然后使用less程序将其分页输出


推荐资料:

http://www.cyberciti.biz/faq/linux-unix-creating-a-manpage/

http://www.linuxhowtos.org/system/creatingman.htm


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值