Unix shell 命令的一般语法是什么? [英] What is the general syntax of a Unix shell command?

查看:29
本文介绍了Unix shell 命令的一般语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是,为什么有时某些命令的选项前面是 + 符号,有时前面是 - 符号?

In particular, why is that sometimes the options to some commands are preceded by a + sign and sometimes by a - sign?

例如:

sort -f
sort -nr
sort +4n
sort +3nr

推荐答案

最近,POSIX 标准使用 getopt()(又名 getopt(3)) 被广泛用作标准符号,但在早期,人们一直在试验.在某些机器上,sort 命令不再支持 + 表示法.但是,各种命令(特别是 artar)接受没有任何前缀字符的控件 - 和 dd(由 Alok 在评论中提到)使用完全是另一个约定.

These days, the POSIX standard using getopt() (aka getopt(3)) is widely used as a standard notation, but in the early days, people were experimenting. On some machines, the sort command no longer supports the + notation. However, various commands (notably ar and tar) accept controls without any prefix character - and dd (alluded to by Alok in a comment) uses another convention altogether.

使用 '--' 作为长选项的 GNU 约定(由 getopt_long(3)) 已从使用 '+' 更改.当然,X11 软件在多字符选项之前使用单破折号.所以,当人们尝试如何最好地处理它时,整个事情就是一个历史遗迹的集合.

The GNU convention of using '--' for long options (supported by getopt_long(3)) was changed from using '+'. Of course, the X11 software uses a single dash before multi-character options. So, the whole thing is a collection of historic relics as people experimented with how best to handle it.

POSIX 记录了它适用的实用约定,除非历史先例更强.

POSIX documents the Utility Conventions that it works to, except where historical precedent is stronger.

[有一次,SO 367309 包含以下材料作为我的答案.最初由 FerranB 于 2008-12-15 02:02 提出,但随后被关闭并删除.]

[At one time, SO 367309 contained the following material as my answer. It was originally asked 2008-12-15 02:02 by FerranB, but was subsequently closed and deleted.]

您认识多少种不同类型的选项?我能想到许多,包括:

How many different types of options do you recognize? I can think of many, including:

  • 以单破折号开头的单字母选项,当存在时可分组没有参数,参数可以附加到选项字母或下一个参数(很多很多 Unix 命令;大多数 POSIX 命令).
  • 以单破折号开头的单字母选项,不允许分组,必须附加参数 (RCS).
  • 以单破折号开头的单字母选项,不允许分组,参数必须是单独的(POSIX 之前的 SCCS、IIRC).
  • 以单破折号开头的多字母选项,参数可以是附加或在下一个参数中(X11 程序;还有 Java 和 Mac OS X 上具有 NeXTSTEP 传统的许多程序).
  • 以单破折号开头的多字母选项,可以缩写(Atria Clearcase).
  • 以单加号开头的多字母选项(已过时).
  • 以双破折号开头的多字母选项;参数可以跟在 '='或分开(GNU 实用程序).
  • 没有前缀/后缀的选项,有些名称有缩写或者是暗示,参数必须分开.(AmigaOS壳)

对于带有可选参数的选项,有时必须附加参数(co -p1.3 rcsfile.c),有时它必须跟在一个="符号之后.POSIX 不支持可选有意义的参数(POSIX getopt() 只允许它们用于最后命令行选项).

For options taking an optional argument, sometimes the argument must be attached (co -p1.3 rcsfile.c), sometimes it must follow an '=' sign. POSIX doesn't support optional arguments meaningfully (the POSIX getopt() only allows them for the last option on the command line).

所有明智的期权系统都使用由双破折号组成的期权('--') 单独表示选项结束"——以下参数是非选项参数"(通常是文件名;POSIX 称它们为操作数")即使他们以短跑.(我认为支持这个符号是必要的.请注意,如果 -- 前面是一个需要参数的选项,则 -- 将被视为选项,而不是作为选项结束"标记.)

All sensible option systems use an option consisting of double-dash ('--') alone to mean "end of options" — the following arguments are "non-option arguments" (usually file names; POSIX calls them 'operands') even if they start with a dash. (I regard supporting this notation as an imperative. Be aware that if the -- is preceded by an option requiring an argument, the -- will be treated as the argument to the option, not as the 'end of options' marker.)

许多但不是所有的程序都接受单破折号作为文件名来表示标准输入(通常)或标准输出(偶尔).有时,与 GNU 'tar' 一样,两者都可以在单个命令行中使用:

Many but not all programs accept single dash as a file name to mean standard input (usually) or standard output (occasionally). Sometimes, as with GNU 'tar', both can be used in a single command line:

... | tar -cf - -F - | ...

第一个单独的破折号表示写入标准输出";第二种意思是读取文件"来自标准输入的名字.

The first solo dash means 'write to stdout'; the second means 'read file names from stdin'.

有些程序使用其他约定——即,选项前面没有短跑.其中许多来自 Unix 最古老的时代.例如,'tar' 和 'ar' 都接受不带破折号的选项,所以:

Some programs use other conventions — that is, options not preceded by a dash. Many of these are from the oldest days of Unix. For example, 'tar' and 'ar' both accept options without a dash, so:

tar cvzf /tmp/somefile.tgz some/directory

dd 命令只使用 opt=value:

dd if=/some/file of=/another/file bs=16k count=200

某些程序允许您交错选项和其他参数完全地;C 编译器、make 和 GNU 实用程序运行时无需环境中的 POSIXLY_CORRECT 就是例子.很多节目都期待其他参数之前的选项.

Some programs allow you to interleave options and other arguments completely; the C compiler, make and the GNU utilities run without POSIXLY_CORRECT in the environment are examples. Many programs expect the options to precede the other arguments.

请注意,git 和其他 VCS 命令通常使用混合系统:

Note that git and other VCS commands often use a hybrid system:

git commit -m 'This is why it was committed'

有一个子命令作为参数之一.通常,可以在命令和子命令之间指定可选的全局"选项.在 POSIX 中有这样的例子;sccs 命令属于这一类;你可以争辩说,运行其他命令的其他一些命令也属于这一类:nicexargs 是从 POSIX 中浮现出来的;sudo 是一个非 POSIX 示例,svncvs 也是如此.

There is a sub-command as one of the arguments. Often, there will be optional 'global' options that can be specified between the command and the sub-command. There are examples of this in POSIX; the sccs command is in this category; you can argue that some of the other commands that run other commands are also in this category: nice and xargs spring to mind from POSIX; sudo is a non-POSIX example, as are svn and cvs.

我对不同的系统没有强烈的偏好.什么时候选项很少,然后是具有助记值的单个字母很方便.GNU 支持这一点,但建议使用前面有双破折号的多字母选项.

I don't have strong preferences between the different systems. When there are few enough options, then single letters with mnemonic value are convenient. GNU supports this, but recommends backing it up with multi-letter options preceded by a double-dash.

有些事情我确实反对.最糟糕的事情之一是一样的选项字母根据其他含义使用不同的含义选项字母在它之前.在我的书中,这是一个禁忌,但我知道完成它的软件.

There are some things I do object to. One of the worst is the same option letter being used with different meanings depending on what other option letters have preceded it. In my book, that's a no-no, but I know of software where it is done.

另一个令人反感的行为是处理方式的不一致参数(特别是对于单个程序,但也适用于一组程式).要么需要附加参数,要么需要分离参数(或允许),但没有一些选项需要附加参数和其他需要独立参数的参数.并关于是否可以使用 '=' 来分隔选项和论据.

Another objectionable behaviour is inconsistency in style of handling arguments (especially for a single program, but also within a suite of programs). Either require attached arguments or require detached arguments (or allow either), but do not have some options requiring an attached argument and others requiring a detached argument. And be consistent about whether '=' may be used to separate the option and the argument.

与很多很多(软件相关的)事情一样——一致性更重要比个人决定更重要.使用自动化工具并标准化参数处理有助于保持一致性.

As with many, many (software-related) things — consistency is more important than the individual decisions. Using tools that automate and standardize the argument processing helps with consistency.

无论您做什么,请阅读TAOUP 的命令行选项 和考虑命令行接口标准.(由 J F 添加塞巴斯蒂安——谢谢;我同意.)

Whatever you do, please, read the TAOUP's Command-Line Options and consider Standards for Command Line Interfaces. (Added by J F Sebastian — thanks; I agree.)

这篇关于Unix shell 命令的一般语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆