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

查看:87
本文介绍了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) <支持)/a>)已从使用'+'更改为.当然,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 提出的,但随后被关闭并删除.]

[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 壳)
  • Single-letter options preceded by single dash, groupable when there is no argument, argument can be attached to option letter or in next argument (many, many Unix commands; most POSIX commands).
  • Single-letter options preceded by single dash, grouping not allowed, arguments must be attached (RCS).
  • Single-letter options preceded by single dash, grouping not allowed, arguments must be separate (pre-POSIX SCCS, IIRC).
  • Multi-letter options preceded by single dash, arguments may be attached or in next argument (X11 programs; also Java and many programs on Mac OS X with a NeXTSTEP heritage).
  • Multi-letter options preceded by single dash, may be abbreviated (Atria Clearcase).
  • Multi-letter options preceded by single plus (obsolete).
  • Multi-letter options preceded by double dash; arguments may follow '=' or be separate (GNU utilities).
  • Options without prefix/suffix, some names have abbreviations or are implied, arguments must be separate. (AmigaOS Shell)

对于带有可选参数的选项,有时必须附加参数(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 - | ...

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

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天全站免登陆