1、内部命令和外部命令:
对shell来说,命令分为内部命令和外部命令,即有shell自身提高的命令即为内部命令(如cd、pwd等),其他为外部命令。那么如何来区分内部命令和外部名呢?使用命令whatis COMMAND
内部命令显示如下:
[root@nagios ~]# whatis cd
cd (1p) - change the working directory
cd [builtins] (1) - bash built-in commands, see bash(1)
有 bash built-in commands, see bash(1)这样一句就表示是内部命令。
外部命令显示如下:
[root@nagios ~]# whatis cat
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
2、获取命令的几种方式:
2.1 对于内部命令,可以使用下面命令获取帮助:
命令用法:help COMMAND
例:
[root@nagios ~]# help cd
cd: cd [-L|-P] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.
2.2 对于外部命令,一般可以使用下列方式获取基本帮助:
命令用法:COMMAND --help
例:
[root@nagios ~]# cp --help
用法:cp [选项]... [-T] 源文件 目标文件
或:cp [选项]... 源文件... 目录
或:cp [选项]... -t 目录 源文件...
将源文件复制至目标文件,或将多个源文件复制至目标目录。
长选项必须使用的参数对于短选项时也是必需使用的。
-a, --archive 等于-dR --preserve=all
--backup[=CONTROL 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
--copy-contents 在递归处理是复制特殊文件内容
-d 等于--no-dereference --preserve=links
……以下略……
2.3 绝大多数linux命令(含内部命令和外部命令),均可以使用man命令获取命令手册
命令用法:man COMMAND
man命令是使用最多的获取帮助命令。
2.4 使用下面命令可以获取命令在线文档:
命令用法:info COMMAND
info一般用于详细说明命令的历史发展情况、基本使用等。
3、man帮助详解:
man命令是使用最多的获取帮助命令,该命令获取到的帮助有SYNOPSIS(简介)、DESCRIPTION(详细使用说明)、FILES(命令涉及到的配置文件)、SEE ALSO(另外参照)、BUGS和EXAMPLES(示例)等。
3.1 man内容按章节区分,每个章节有固定的内容,主要如下:
1:用户命令(/bin, /usr/bin, /usr/local/bin)
2:系统调用
3:库用户
4:特殊文件(设备文件)
5:文件格式(配置文件的语法)
6:游戏
7:杂项(Miscellaneous)
8: 管理命令(/sbin, /usr/sbin, /usr/local/sbin)
3.2 查看命令章节:
一个命令可能有其中一个或多个章节,通过上面介绍过的whatis命令可以看到命令包含哪些章节,具体如下:
[root@nagios ~]# whatis cp
cp (1) - copy files and directories
cp (1p) - copy files
如上,cp只有第一个章节
[root@nagios ~]# whatis mount
mount (2) - mount file system
mount (8) - mount a filesystem
mount.cifs [mount] (8) - mount using the Common Internet File System (CIFS)
mount.ecryptfs [mount] (8) - eCryptfs mount helper
mount.ecryptfs_private [mount] (1) - eCryptfs private mount helper
mount.nfs [mount] (8) - mount a Network File System
mount.nfs4 [mount] (8) - mount a Network File System
如上,mount命令具有第二章节和第八章节:
[root@nagios ~]# whatis passwd
passwd (1) - update user's authentication tokens
passwd (5) - password file
passwd [sslpasswd] (1ssl) - compute password hashes
passwd具有第一章节和第五章节,通过whatis命令直接就可以看出,第一章节的是说明更新用户密码的,第五章节是说明passwd这个配置文件的。
运行man passwd
[root@nagios ~]# man passwd
PASSWD(1) User utilities PASSWD(1)
NAME
passwd - update user’s authentication tokens……以下略……
3.3 查看指定章节
如上,man passwd默认显示了第一章节的帮助手册,如果需要查看的是/etc/passwd这个文档的配置语法帮助,可直接查看第五章节,命令如下:
[root@nagios ~]# man 5 passwd
PASSWD(5) Linux Programmer's Manual PASSWD(5)
NAME
passwd - password file……以下略……
3.4 man信息的主要内容:
NAME:命令名称及功能简要说明
SYNOPSIS:用法说明,包括可用的选项
DESCRIPTION:命令功能的详尽说明,可能包括每一个选项的意义
OPTIONS:说明每一个选项的意义
FILES:此命令相关的配置文件
BUGS:
EXAMPLES:使用示例
SEE ALSO:另外参照
3.5 SYNOPSIS中选项说明:
如下列cp的SYNOPSIS部分:
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
选项多用如下符号标识:
<>:必选
[]:可选
...:可以出现多次
|:多选一
{}:分组
3.6 善用EXAMPLES:
对那些需要很多选项或参数的命令,初学者即使看到完整的帮助也一时半会难以搞清楚怎么使用,其实很多命令的man帮助都提供了EXAMPLES,如挂载iscsi磁盘,使用man命令查看帮助如下:
[root@nagios ~]# man iscsiadm
对于上述这么多选项和参数,初学者大多看到就头痛了,即使后面有详细说明,但也很难很好的组合使用,这是就可以看EXAMPLES:
小技巧:输入/EXAMPLES 后按回车键,即可快速跳转到EXAMPLES部分
EXAMPLES
Discover targets at a given IP address:iscsiadm --mode discoverydb --type sendtargets --portal 192.168.1.10 --discover
Login, must use a node record id found by the discovery:
iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260 --login
Logout:
iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260 --logout
List node records:
iscsiadm --mode node
Display all data for a given node record:
iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260
通过EXAMPLES,我们就可以知道发现iscsi目标命令为:
iscsiadm --mode discoverydb --type sendtargets --portal 192.168.1.10 --discover
挂载命令为:
iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.1:3260 --login
我们在使用时只需将上述ip或targetname替换为我们自己的即可,很方便吧。
3.7 man阅读使用技巧:
前面已经说了一个查询和快速跳转的技巧,下面再说其他几个基本使用技巧:
翻屏:
向后翻一屏:SPACE
向前翻一屏:b
向后翻一行:ENTER
向前翻一行:k
查找:
/KEYWORD: 向后 ,上面已使用过一次
n: 下一个
N:前一个?KEYWORD:向前
n: 下一个
N:前一个
q: 退出