您的位置:首页 > 运维架构 > Shell

Shell 命令的语法

2016-09-14 12:58 525 查看
如何学习一条Unix-like 命令:命令的结构组成:(1,命令本身 2,选项 3,参数 )

A good approach to learning a new command is to answer the following three questions:

•  What does the command do?
•  How do I use the options?
•  How do I use the arguments?


① 如何读懂命令的语法?

有规则六:

1.Items in square brackets[] are optional.
2.Items not in square brackets are obligatory.
3.Anything in boldface must be typed exactly as written.
4.Anything in italics must be replaced by an appropriate value.
5.An argument followed by an ellipsis (...) may be repeated any number of times.
6.If you see a single option grouped with an argument, the option and argument must be used together.


案例:

$ man [-P pager] [-S sectionlist] name..

In this case, if you want to use the -P option, it must be immediately followed by the argument pager.

Similarly,if you want to use the -S option, it must be immediately followed by the argument sectionlist.

In this case, it is possible for the pager argument to come before the -S option.–因此,paper参数在 -S 选项前面是可以的。

② 命令的参数个数问题

–学习命令要确认命令的参数情况:

1.零个或多个参数。

2.一个或多个参数。

如果命令是 1 规则,那么一定得确认,命令的默认参数是什么。

–案例:

$ ls
默认显示当前目录的内容。




试问斗胆如下操作会有何结果:

$ cd / ; rm -rf


③ ‘-‘选项和’–’选项

大多数的Unix-like系统只有单字符选项。

而linux系统,使用的GNU工具,还可以见到有长选项。

$ ls -help
-help选项会被解释为 4 个独立的 -h-e-l-p选项。



$ ls --reverse
ls -r
两个命令式等价的,是一个选项!

–note:

1.大多数的重要选项为 短选项。

2.大多数的长选项是短选项的同义词。

3.’-‘选项,易于敲打。

4.’–’选项,多个字符选项易于理解,记忆!

Gnu工具有两个常见的长选项:

–help : 显示命令的语法摘要!

–version :显示系统上所安装的程序的版本信息!

因此,在linux系统上,你可以尝试,–version选项,

看看,所使用的工具是否就是GNU:

find --version
ls --version
\time --version


如果是,那么可使用 –help来获取命令的使用方式。

④ 命令,选项,参数的间隔问题?

Unix shell 被设计的十分灵活,对命令行各个部分有多少个空格并不介意,只要词与词之间分开可以!

输入命令时,每个选项及参数要用 空白符(意味着任意个 空格/制表符/新行字符),隔开!

–note:

空白符的概念: 一个或多个连续的空格,制表符或新行字符。

⑤ 同时操作多条命令

一条命令行可输入多个命令,使用 分号 “;” 将命令隔开就行了!

$ date; sleep 10s ; date


–文章来自于Unix教程的学习笔记。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Unix-命令学习