- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
bash 手册列出了 for
的语法。复合语句为for name [ [ in [ word ... ] ] ; ] do list ; done
这意味着 do
之前的分号如果 in
是可选的子句省略。 [笔记2]。
但是,Posix 规范仅列出了 for_clause
的以下三个产生式:
for_clause : For name linebreak do_group
| For name linebreak in sequential_sep do_group
| For name linebreak in wordlist sequential_sep do_group
;
供引用,
linebreak
是一个可能为空的序列
NEWLINE
而
sequential_sep
是分号或
NEWLINE
, 可能后跟
NEWLINE
的序列:
newline_list : NEWLINE
| newline_list NEWLINE
;
linebreak : newline_list
| /* empty */
;
separator : separator_op linebreak
| newline_list
;
sequential_sep : ';' linebreak
| newline_list
;
据我所知,这禁止语法
for foo; do :; done
.
for foo; do :; done
和
for foo do :; done
毫无怨言,无论 Posix 还是他们自己的文档 [注 3]。
for loop
的 XCU 描述中, Posix 似乎坚持换行:
The format for the for loop is as follows:
for name [ in [word ... ]]
do
compound-list
done
The format is shown with generous usage of <newline> characters. See the grammar in XCU Shell Grammar for a precise description of where <newline> and <semicolon> characters can be interchanged.
bash
手册对换行符并不完全明确;它说的是:In most cases a list in a command's description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.
done
前面的分号可以用换行符代替,但似乎没有提到可以对do
前面的分号进行同样的转换.ksh
和 zsh
似乎坚持在 name
之后有一个分号或换行符,虽然实现并不坚持它。ksh
手册页列出的语法如下:
for vname [ in word ... ] ;do list ;done
;do
和 ;done
中的分号代表“分号或换行符”。我找不到任何明确的声明,但这是理解语法描述的唯一方法。)zsh
手册显示:
for name ... [ in word ... ] term do list done
where term is at least one newline or ;.
最佳答案
好找!我没有明确的答案,但以下是源代码对此的说明:
它确实在 the original Bourne shell 中无效来自 AT&T UNIX v7:
(shell has just read `for name`):
IF skipnl()==INSYM
THEN chkword();
t->forlst=item(0);
IF wdval!=NL ANDF wdval!=';'
THEN synbad();
FI
chkpr(wdval); skipnl();
FI
chksym(DOSYM|BRSYM);
in
的一部分处理的分号的副作用。组,当没有“in”时完全跳过。
/*
* Newline or semicolon here is optional (but note
* that the original Bourne shell only allowed NL).
*/
/* 'for i;do cmd' is valid syntax */
else if(tok==';')
while((tok=sh_lex(lexp))==NL);
for_command: FOR WORD newline_list DO compound_list DONE
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
...
| FOR WORD ';' newline_list DO compound_list DONE
{
$$ = make_for_command ($2, add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6, word_lineno[word_top]);
if (word_top > 0) word_top--;
}
while (tok == SEPER)
zshlex();
SEPER
是
;
or linefeed )。因此,zsh 愉快地接受了这个循环:
for foo; ;
;
; ; ; ; ;
; do echo cow; done
关于shell - `for NAME do ...` 中的 NAME 后是否禁止使用分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896830/
;用作语句分隔符,所以放置多个;在语句的末尾很好,因为它只是添加了空语句。 我遇到了这个有多个 ; 的代码最后但删除它们会导致错误: $line =~s;[.,]$;;; 应该与 $line =~s;
当使用自动完成来填充方法参数时,大多数情况下最后应该有一个分号。 似乎必须有一些键盘快捷键可以在最后输入分号(也可能是换行符),但我找不到它! 谢谢。 编辑 : 也许问题不清楚,当完成一个自动完成方法
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我知道我不应该在循环后放置分号。但我正在学习,不小心插入了一个。我想确切地知道我的错误到底发生了什么。所以下次发生类似的事情时,我就知道错误的根源。 在下面的代码中,在这部分代码中: triangul
我想拆分我的查询,但没有得到完全符合我要求的答案。 我的字符串如下: select 1;select \\2; select 3\\;copy customer from 's3://mybucket
VBA 中带有 Debug.Print 语句的分号 ( ; ) 的实际值/含义是什么? 我经常使用它,但对它没有真正的了解,我所知道的是它不等于 vbTab 常量,因为间距不一样。 IE。 Debug
我需要从 HTML 中的 javascript 代码获取分配的变量名称和值。 例如有html代码: ~~ contents var value1 = 55;var value2= 27;
我正在用 JSFiddle 校对这个函数,它说第二行缺少分号,但是我无法确定它会去哪里。 function debtSpiral() { var debtID = setInterval(debt
给定一个使用 ; 策略的有效 Coq 证明,是否有一个通用公式可以将其转换为用 . 替换 ; 的有效等效证明;? 许多 Coq 证明使用 ; 或战术排序策略。作为初学者,我想观察各个步骤的执行,因此我
我被困在工作中,电脑被锁定。但我正在尝试练习我的 Scala。我正在使用 Ideone.com,因为我什至无法安装 scalac... 无论如何这不是编译: class DPt(var name: S
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
考虑这些文件名。 ; f - Copy.txt ;f.txt f - Copy ;- Copy.txt f - Copy.txt 我有这段代码可以解析任何文件中的 & 符号,答案是:Drag and
为什么带有验证器(Mozilla 或 JSlint)的 Aptana 会提示这段代码: var collectionOfValues = { key0 : value0; key1 :
有什么方法可以绑定(bind); (\059) 到 tmux 中的命令? 默认情况下,它绑定(bind)到最后一个 Pane ;但是,我想将其重新绑定(bind)到 select-pane -R。 我
我有一个 Java 字符串,它实际上是一个 SQL 脚本。 CREATE OR REPLACE PROCEDURE Proc AS b NUMBER:=3; c
我有一个 python 字符串: names = "John, Paul; Sally/Frank" 我想用 , 分割字符串;/.我试过: names.split(",") 但我不确定如何通过所有分隔
在声明一组变量时使用逗号而不是分号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
在声明一组变量时使用逗号而不是分号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
我一直在尝试并查看各种命令,但似乎无法找到正确的语法; 我想合并两个 ls 命令的输出,除以 ;(分号)并将输出写入文件。 我的 ls 命令是: ls *.lst ls -d -1 $PWD/*.ls
在声明一组变量而不是分号时使用逗号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
我是一名优秀的程序员,十分优秀!