第5章:取得指令使用说明帮助方法

前言

在上一章节中,学会了基本的档案与目录之管理以及档案系统的目录架构之後,在这个章节中,要教导如何取得指令的帮助与用法的说明。

man指令介绍

man指令是一种可以查询各个在系统上指令说明的方式,通常常见的Linux手册指印的类型如下清单:

  • 若指定section为1,则内容类型为使用者命令有可能是执行档或者是shell脚本。
  • 若指定section为2,则内容类型为系统呼叫,像是Kernel用的例行程序呼叫等。
  • 若指定section为3,则内容类型为系统上面的函式库之用法,通常是给开发系统程序之开发者所使用。
  • 若指定section为4,则内容类型为特殊档案,像是装置设备档案等。
  • 若指定section为5,则内容类型为档案格式,像是系统上之软件套件的设定档说明等。
  • 若指定section为6,则内容类型为一些有趣的程序或是指令的用法说明。
  • 若指定section为7,内容类型为通常指的是协定或是档案系统的说明。
  • 若指定section为8,内容类型为系统管理和只能是最高权限使用者才可以执行指令说明,像是正在被系统维护的任务等。
  • 若指定section为9,内容类型为Linux Kernel API之相关用法说明,像是内部Kernel在呼叫的指令或是函式等。

在man pages操作方式

当进入到指令搜寻的说明页面时候,会在终端机上进入到一个阅读器,这时候需要使用到键盘进行浏览与查看,以下是笔者整理出几个在阅读器中操作与观看说明文件的方法:

  • Spacebar,即空白键,可以在阅读器中将说明页面往下卷动。
  • Pagedown,即键盘上的「Pagedown」键,可以在阅读器中以画面方式将说明页面往下卷动。
  • Pageup,即键盘上的「Pageup」键,可以在阅读器中以画面方式将说明页面往上卷动。
  • DownArrow,即键盘上的往下键,可以在阅读器中将说明页面往下移动一行。
  • UpArrow,即键盘上的往上键,可以在阅读器中将说明页面往上移动一行。
  • D,即键盘中「D」键,可以在阅读器中将说明页面往下移动画面中之一半。
  • U,即键盘中「U」键,可以在阅读器中将说明页面往上移动画面中之一半。
  • /string,可以在阅读器中,按下「/」键并可以输入要在说明页面中找寻的字串内容,输入完搜寻字串後,按下「enter」键即可已开始搜寻。
  • N,即键盘中的「N」键,可以在完上述的搜寻动作之後,按下这个键则可以找寻下一个指定的搜寻内容所在的行数。
  • Shift+N,即键盘中同时按下「Shift」与「N」键,可以在完上述的搜寻动作之後,按下这个键则可以找寻上一个指定的搜寻内容所在的行数。
  • G,即键盘之「G」键,则是回到阅读器中之说明页面最一开始第一行。
  • Shift+G,即键盘同时按下「Shf0t」与「G」键,则是回到阅读器中之说明页面之最後第一行。
  • Q,即键盘之「Q」键,离开阅读器中之说明页面。

阅读说明页面方法

当进入到指令的说明页面,都有一个固定格式,以下以清单的方式整理出格式之说明:

  • NAME:标题名称,通常是指令名称或是档案名称。
  • SYNOPSIS:指令语法的概要。
  • DESCRIPTION:指令的描述,这边会有一两句来描述这个指令。
  • OPTIONS:指令执行时候可以代的参数方式说明。
  • EXAMPLES:一些指令如何使用的范例。
  • FILES:列出与这个man pages有关的档案与附录清单。
  • SEEALSO:列出其他相关的资讯,可能是附上其他man page之标题。
  • BUGS:一些这个指令或是软件套件的臭虫错误。
  • AUTHOR:一些有关於这个软件套件或是指令之作者资讯。

在man page中寻找关键字

若是使用man ls指令,则是利用man找到ls这个指令的说明与使用方式,若是带上参数-k则是寻找有关於passwd这个关键字有关的说明页面,相关执行後的输出如下所示:

[rockylinux@workstation ~]$ man -k passwd
passwd: nothing appropriate.

出现这个原因是因为man这个指令有一个资料库叫做mandb,这个mandb并没有做更新或者是更新不完全,为了要让man指令可以运作的更好,可以使用sudo mandb -c来将man指令所需要用到的资料库再进行更新一次,因为更新mandb资料库需要是root使用者最高权限才可以更新,而这个使用者刚好可以使用sudo指令来暂时取得最高使用者权限进行mandb之更新,相关更进一步有关於sudo的介绍,会在後面章节进行介绍;相关的更新结果之输出如下所示:

[rockylinux@workstation ~]$ sudo mandb -c
Processing manual pages under /usr/share/man/overrides...
Updating index cache for path `/usr/share/man/overrides/man8'. Wait...done.
Checking for stray cats under /usr/share/man/overrides...
Checking for stray cats under /var/cache/man/overrides...
Processing manual pages under /usr/share/man...
Updating index cache for path `/usr/share/man/man1'. Wait...mandb: warning: /usr/share/man/man1/gdk-pixbuf-query-loaders.1.gz: whatis parse for gdk-pixbuf-query-loaders(1) failed
Updating index cache for path `/usr/share/man/man5'. Wait...mandb: can't open /usr/share/man/man5/regulatory.bin.5.gz: No such file or directory
mandb: warning: /usr/share/man/man5/regulatory.db.5.gz: bad symlink or ROFF `.so' request
mandb: warning: /usr/share/man/man5/containers-signature.5.gz: whatis parse for containers-signature(5) failed
Updating index cache for path `/usr/share/man/man3'. Wait...mandb: warning: /usr/share/man/man3/Net::libnetFAQ.3pm.gz: whatis parse for Net::libnetFAQ(3pm) failed
Updating index cache for path `/usr/share/man/mann'. Wait...done.
Checking for stray cats under /usr/share/man...
Checking for stray cats under /var/cache/man...
Processing manual pages under /usr/share/man/overrides...
Updating index cache for path `/usr/share/man/overrides/man8'. Wait...done.
Checking for stray cats under /usr/share/man/overrides...
Checking for stray cats under /var/cache/man/overrides...
Processing manual pages under /usr/share/man/ru...
Updating index cache for path `/usr/share/man/ru/man7'. Wait...done.
Checking for stray cats under /usr/share/man/ru...
Checking for stray cats under /var/cache/man/ru...
Processing manual pages under /usr/share/man/hu...
Updating index cache for path `/usr/share/man/hu/man8'. Wait...done.
Checking for stray cats under /usr/share/man/hu...
Checking for stray cats under /var/cache/man/hu...
Processing manual pages under /usr/share/man/ja...
Updating index cache for path `/usr/share/man/ja/man7'. Wait...done.
Checking for stray cats under /usr/share/man/ja...
Checking for stray cats under /var/cache/man/ja...
Processing manual pages under /usr/share/man/fr...
Updating index cache for path `/usr/share/man/fr/man7'. Wait...done.
Checking for stray cats under /usr/share/man/fr...
Checking for stray cats under /var/cache/man/fr...
Processing manual pages under /usr/share/man/it...
Updating index cache for path `/usr/share/man/it/man8'. Wait...done.
Checking for stray cats under /usr/share/man/it...
Checking for stray cats under /var/cache/man/it...
Processing manual pages under /usr/share/man/pl...
Updating index cache for path `/usr/share/man/pl/man5'. Wait...done.
Checking for stray cats under /usr/share/man/pl...
Checking for stray cats under /var/cache/man/pl...
Processing manual pages under /usr/share/man/ko...
Updating index cache for path `/usr/share/man/ko/man8'. Wait...done.
Checking for stray cats under /usr/share/man/ko...
Checking for stray cats under /var/cache/man/ko...
Processing manual pages under /usr/share/man/sk...
Updating index cache for path `/usr/share/man/sk/man8'. Wait...done.
Checking for stray cats under /usr/share/man/sk...
Checking for stray cats under /var/cache/man/sk...
Processing manual pages under /usr/share/man/cs...
Updating index cache for path `/usr/share/man/cs/man7'. Wait...done.
Checking for stray cats under /usr/share/man/cs...
Checking for stray cats under /var/cache/man/cs...
Processing manual pages under /usr/share/man/da...
Updating index cache for path `/usr/share/man/da/man8'. Wait...done.
Checking for stray cats under /usr/share/man/da...
Checking for stray cats under /var/cache/man/da...
Processing manual pages under /usr/share/man/de...
Updating index cache for path `/usr/share/man/de/man8'. Wait...done.
Checking for stray cats under /usr/share/man/de...
Checking for stray cats under /var/cache/man/de...
Processing manual pages under /usr/share/man/id...
Updating index cache for path `/usr/share/man/id/man5'. Wait...done.
Checking for stray cats under /usr/share/man/id...
Checking for stray cats under /var/cache/man/id...
Processing manual pages under /usr/share/man/pt_BR...
Updating index cache for path `/usr/share/man/pt_BR/man8'. Wait...done.
Checking for stray cats under /usr/share/man/pt_BR...
Checking for stray cats under /var/cache/man/pt_BR...
Processing manual pages under /usr/share/man/sv...
Updating index cache for path `/usr/share/man/sv/man7'. Wait...done.
Checking for stray cats under /usr/share/man/sv...
Checking for stray cats under /var/cache/man/sv...
Processing manual pages under /usr/share/man/tr...
Updating index cache for path `/usr/share/man/tr/man7'. Wait...done.
Checking for stray cats under /usr/share/man/tr...
Checking for stray cats under /var/cache/man/tr...
Processing manual pages under /usr/share/man/zh_CN...
Updating index cache for path `/usr/share/man/zh_CN/man8'. Wait...done.
Checking for stray cats under /usr/share/man/zh_CN...
Checking for stray cats under /var/cache/man/zh_CN...
Processing manual pages under /usr/share/man/zh_TW...
Updating index cache for path `/usr/share/man/zh_TW/man8'. Wait...done.
Checking for stray cats under /usr/share/man/zh_TW...
Checking for stray cats under /var/cache/man/zh_TW...
Processing manual pages under /usr/share/man/es...
Updating index cache for path `/usr/share/man/es/man1'. Wait...done.
Checking for stray cats under /usr/share/man/es...
Checking for stray cats under /var/cache/man/es...
Processing manual pages under /usr/share/man/uk...
Updating index cache for path `/usr/share/man/uk/man1'. Wait...mandb: warning: /usr/share/man/uk/man1/dos2unix.1.gz: whatis parse for dos2unix(1) failed
mandb: warning: /usr/share/man/uk/man1/dos2unix.1.gz: whatis parse for mac2unix(1) failed
mandb: warning: /usr/share/man/uk/man1/dos2unix.1.gz: whatis parse for unix2dos(1) failed
mandb: warning: /usr/share/man/uk/man1/dos2unix.1.gz: whatis parse for unix2mac(1) failed
Updating index cache for path `/usr/share/man/uk/man7'. Wait...done.
Checking for stray cats under /usr/share/man/uk...
Checking for stray cats under /var/cache/man/uk...
Processing manual pages under /usr/share/man/ca...
Updating index cache for path `/usr/share/man/ca/man8'. Wait...done.
Checking for stray cats under /usr/share/man/ca...
Checking for stray cats under /var/cache/man/ca...
Processing manual pages under /usr/share/man/nl...
Updating index cache for path `/usr/share/man/nl/man1'. Wait...done.
Checking for stray cats under /usr/share/man/nl...
Checking for stray cats under /var/cache/man/nl...
Processing manual pages under /usr/share/man/pt...
Updating index cache for path `/usr/share/man/pt/man8'. Wait...done.
Checking for stray cats under /usr/share/man/pt...
Checking for stray cats under /var/cache/man/pt...
Processing manual pages under /usr/local/share/man...
Updating index cache for path `/usr/local/share/man/mann'. Wait...done.
Checking for stray cats under /usr/local/share/man...
Checking for stray cats under /var/cache/man/local...
124 man subdirectories contained newer manual pages.
7794 manual pages were added.
0 stray cats were added.

从上述输出的更新过程中,有时候有些失败讯息是因为有些档案找不到因此更新失败,那个错误讯息可以忽略,因为有可能真的没有那个mandb档案可以做更新,更新完之後,再执行一次man -k passwd指令,则会有下列正常的输出结果了:

[rockylinux@workstation ~]$ man -k passwd
chgpasswd (8)        - update group passwords in batch mode
chpasswd (8)         - update passwords in batch mode
fgetpwent_r (3)      - get passwd file entry reentrantly
getpwent_r (3)       - get passwd file entry reentrantly
gpasswd (1)          - administer /etc/group and /etc/gshadow
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1)          - Change group or user password
openssl-passwd (1ssl) - compute password hashes
pam_localuser (8)    - require users to be listed in /etc/passwd
passwd (1)           - update user's authentication tokens
passwd (5)           - password file
passwd2des (3)       - RFS password encryption
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
saslpasswd2 (8)      - set a user's sasl password
smbpasswd (5)        - The Samba encrypted password file
sslpasswd (1ssl)     - compute password hashes
vncpasswd (1)        - change the VNC password

接下来使用man ls来看一下,可以看到以下的部分说明页面如下:

LS(1)                                               User Commands                                               LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about the FILEs (the current directory by default).  Sort entries alphabetically if none of
       -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              with -l, scale sizes by SIZE when printing them; e.g., '--block-size=M'; see SIZE format below
.....

从上面的说明页面来看,ls [OPTION]... [FILE]...的说明如下:

  • []之中括号起来的选项,意思是在执行指令的时候可以带或不带选项,即可有可无的意思。
  • ...的意思是说,在执行指令的後,带选项可以是重复的。

接着使用man ps2pdf指令来看ps2pdf指令的说明页面,相关节录的说明页面如下:

PS2PDF(1)                                            Ghostscript                                            PS2PDF(1)

NAME
       ps2pdf - Convert PostScript to PDF using ghostscript
       ps2pdf12 - Convert PostScript to PDF 1.2 (Acrobat 3-and-later compatible) using ghostscript
       ps2pdf13 - Convert PostScript to PDF 1.3 (Acrobat 4-and-later compatible) using ghostscript
       ps2pdf14 - Convert PostScript to PDF 1.4 (Acrobat 5-and-later compatible) using ghostscript

SYNOPSIS
       ps2pdf  [options...] {input.[e]ps|-} [output.pdf|-]
       ps2pdf12  [options...] {input.[e]ps|-} [output.pdf|-]
       ps2pdf13  [options...] {input.[e]ps|-} [output.pdf|-]
       ps2pdf14  [options...] {input.[e]ps|-} [output.pdf|-]

DESCRIPTION
       The  ps2pdf  scripts  are work-alikes for nearly all the functionality (but not the user interface) of Adobe's
       Acrobat(TM) Distiller(TM) product: they convert PostScript files to Portable Document Format (PDF) files.

       If the output filename is not specified, the output is placed in a file of the same name with a '.pdf'  exten‐
       sion  in the current working directory. Either the input filename or the output filename can be '-' to request
       reading from stdin or writing to stdout, respectively, when used as a filter.

       The three scripts differ as follows:

       -      ps2pdf12 will always produce PDF 1.2 output (Acrobat 3-and-later compatible).

       -      ps2pdf13 will always produce PDF 1.3 output (Acrobat 4-and-later compatible).

       -      ps2pdf14 will always produce PDF 1.4 output (Acrobat 5-and-later compatible).

其中,ps2pdf [options...] {input.[e]ps|-} [output.pdf|-]指令意思是,{}意思是,这个选项一定要使用,{}中间必要选其中一个选项来使用,里面的选项以|来隔开。

输出说明页面

平常在一般的情况下,都是使用man 要查询的指令名称这样方式输出指定要查询的说明页面进行查询,若要将整个指令说明页面输出也是可以的,可以使用:man -t ls > ls.ps这样的方式将ls指令说明手册页输出成postscript档案,若要让影印机印出,则可以使用ps2pdf方式将前面输出的postscript档案输出成PDF档,相关使用指令的方式以及输出後的执行结果如下:

[rockylinux@workstation ~]$ man -t ls > ls.ps
[rockylinux@workstation ~]$ file ls.ps
ls.ps: PostScript document text conforming DSC level 3.0
[rockylinux@workstation ~]$ ps2pdf ls.ps ls.pdf
[rockylinux@workstation ~]$ file ls.pdf
ls.pdf: PDF document, version 1.4

从上述来看,多了使用file这个指令来协助查看输出的档案格式资讯,可以看到的是,一开始先用man指令输出ls说明手册页之postscript档案,而再将ls.ps档案输出成ls.pdf之PDF档,为什麽要用file档案检查输出的格式,原因是用.隔开後面的附档名在Linux作业系统中只是因为便於使用者作辨识,实际上在作业系统中档名并没有附档名的概念,如果档案名称乱取的话,还是只能使用file指令来确认真正的档案格式资讯是什麽,下面的例子可以很显着的说明这件事:

[rockylinux@workstation ~]$ man -t ls > ls.txt
[rockylinux@workstation ~]$ file ls.txt
ls.txt: PostScript document text conforming DSC level 3.0

从上面这个例子来看,就可以证明Linux作业系统之世界中,附档名并不能够决定一个档案格式的资讯,而是需要用file指令协助我们看真正的档案资讯,便於识别档案,通常都会用附档名可以对应到真正的档案格式。

课後练习

  • 打开VirtualBox并启动workstation虚拟主机。
  • 在桌面环境打开终端机并透过man指令找到fdisk指令之说明页面。
  • 假设知道su指令是在section 1,透过终端机输入man指令并指定section来找到su指令说明页面。
  • su指令说明页面透过终端机使用man指令输出成Postscript档案并将输出的档案取名叫:su.ps
  • 将上述的su.ps档案转成PDF档并将输出的PDF档案格式取名叫su.pdf

<<:  Day15 购物车 -- 基础结构

>>:  大共享时代系列_005_共享宠物资讯

Day 4:谈谈 docker 的 restart policy

经过昨天的一番努力,我们已经可以在服务无法存取的时候收到通知,那麽今天就来看看另一个议题:服务的重启...

04 - Tmux - 终端机管理工具

在开发时,常常需要多个指令同时运作(例如一个启动前端专案、一个启动後端专案),因此会需要同时开启多个...

【课程推荐】2021/3/13~3/14 软件需求塑模与需求规格文件撰写实务班

课程目标 了解系统分析实务、系统分析工具之应用、逻辑资料库设计技巧、系统分析产出文件、同仁审查等,以...

[Day07] JavaScript - 回圈_part 1

for回圈 我们一般会使用for回圈进行某些次数的回圈,(回圈次数确定时) 一个for回圈会不断重复...

DAY3 起手式--Nuxt.js路由设定

Nuxt.js 跟 Vue-Router 是什麽关系? 有使用过 vue-router 的捧由,别紧...