In-depth analysis of Linux Man commands

Source: Internet
Author: User
Tags cron script

Man has the following eight modules.

1. commands available to users in Shell
2. Use system calls available to programs in the function library
3. library functions available in the program
4. Available devices in the/dev directory
More than 5 Miscellaneous system files (ex:/etc /)
6. game programs
7 Miscellaneous information
8. commands available for Administrators

1) How does the man command search the Help File mapped to the command?
2) What is the format and structure of the Help file?
3) how to create a man help file?
4) What is the running process of whatis and apropos?

First question:
Man command search process
Step 1: Specify the path of the $ PATH variable
Step 2: the path specified in manpath in the/etc/man. config file

Procedure:
1) The program first tries to load the/etc/man. config file and read the content of man. config into the memory,
2) replace man. in the config file, "mansect 1: 8: 2: 3: 4: 5: 6: 7: 9: TCL: N: L: P: O" is read into the memory.
Mansect is the order in which man searches. For example, if the preceding order is found for shell commands and system calls, the shell command is printed first.
3) use man. config and $ path to determine the directory to be searched.
4) use the fstat system to check the existing Directory.
5) determine the module mapped to the searched command/function. For example, the sync command order is, which can be confirmed by man-a sync.
6) Find the man help file based on the ing module. Open the directory in step 1, search for the help files in turn (for the order, see the man command search process)

Note:
1) in the first step of loading man. config, how can I find the/etc/man. config file? The program will continue to run and only search for help files based on $ path?
2) You can add a custom search directory to man. config. The man program checks whether the man directory and man directory exist in the search directory.
For example, if the/tmp directory is defined, the man program will search for/tmp/man and/tmp three directories.
3) Man. config defines the compression and decompression methods. However, if man. config is unreadable, man can still decompress the Help file compressed with gzip.
The man program cannot extract files in other compression formats, for example, help files compressed with bunzip2.
4) use the manpath command to view the path specified by manpath in man. config, but this does not include the path specified in the variable path.

Second question:
The formats of Help Files Read by man programs are divided into two categories.
The first type is the source program written by troff.
The second type is the source program compressed by the compression tool.

The format that the man program can directly read is the first class, and the help file compressed with gzip in the second class.
Other formats can be viewed only by using the decompression tool defined in man. config.
For example:
. GZ/bin/gunzip-C
. Bz2/usr/bin/Bzip2-c-d
. Z
. Z/bin/zcat
. F
. Y
In this section, the .gz format is read directly using gunzip-C, and .bz2 is read using Bzip2-c-d, and. Z is read using/bin/zcat.

The default path of the Help file is/usr/share/man and/usr/local/man/
Among them,/usr/share/man stores the vast majority of help files, and all are compressed help files.
/Usr/local/man stores all help files in text format.

The sub-directories in/usr/share/man are reflected modules. For example, the Help file stored in the Man1 directory is "user-available commands in shell"
The file name is the command name/function name plus the serial number of the module. For compression format, add the compressed extension.
For example:
Host.1.gz is the program name (host). Module No. (1). Extension (GZ)

Third question:
For example, write your own test program Hello. C.
As follows:

# Include <stdio. h>
Int main ()
{
Printf ("Hello world, this is a test! \ N ");
Return 0 ;};

Compile:
Gcc-O hello. c

Create a man file in/usr/share/man.
The file name must be standardized. In this example, hello.1 is used.
The file content is as follows:
. \ "$ ID: hello.1, V 1.11.2.2 2009/06/7 04:44:38 Marka exp $
.\"
. Th "hello" "1" "Jun 7, 2009" "hacker """
. Sh name
Hello \-this is a test. Display Hello strings.
. Sh synopsis
. SP
\ Fbhello \ fr
. Sh "Description"
. PP
\ Fbhello \ fr
Is a simple test for show man program.
\ Fbhello \ fr
Prints a short test message.
. PP
Print follow:
Hello world. This is a test.
. Sh "Files"
. PP
\ Fbhello \ fr
. Sh "See also"
. PP
\ Fbprintf \ FR (1 ),
\ Fbstdio. h \ FR (8 ).

In this way, the man file can be created for the hello program. Of course, you can run Man Hello without a hello executable program.

Now you can use man-Aw hello to view the man file of the hello command.

Fourth question:
The running process of whatis is as follows:
1) Search for the/var/Cache/man/whatis file.
/Var/Cache/man/whatis is a text file.
2) If/var/Cache/man/whatis does not exist, go to the path specified by manpath in man. config to search for the whatis file.

Note:
1) Unlike the man command, the man command First searches for the path specified by the PATH variable.
2) If two whatis files are found, the same command is displayed twice.
For example:
Whatis ls
Ls (1)-list directory contents
Ls (1)-list directory contents
3) create a new man file, such as hello.1 above. At this time, whatis cannot find the matching information, because it has not been synchronized to the/var/Cache/man/whatis file.
To synchronize data, use makewhatis-u-W to update the whatis file.
4) there is a makewhatis. cron script in the/etc/cron. daily directory. Its task is to update the whatis file every day.

Apropos runs in the same way as whatis, but apropos uses the whatis file for full-text search.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.