一个bash循环用大括号? [英] A bash loop with braces?

查看:171
本文介绍了一个bash循环用大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现这一奇怪现象并不能完全弄清楚为什么它的工作原理:

I stumbled across this oddity and can't quite figure out why it works:

$ for ((i=0;i<8;i++)) {
>   printf '%d...' "$i";
> }; echo
0...1...2...3...4...5...6...7...

A 在bash循环使用的括号的语法?我不能重现此使用非算术循环可言,和联机帮助页帮助似乎都和要求不要

A for loop in bash using brace syntax? I can't reproduce this using a non-arithmetic for loop at all, and the manpage and help both seem to require the do:

$ help for
<SNIP>
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
    Arithmetic for loop.

    Equivalent to
        (( EXP1 ))
        while (( EXP2 )); do
            COMMANDS
            (( EXP3 ))
        done

嗯,所以如果我只是...

Hmm, so if I just…

$ ((i=0)); while ((i<8)) { printf '%d...' "$i"; ((i++)); }
-bash: syntax error near unexpected token `{'

都能跟得上!

这是支撑 for循环语法


  1. 记录任何地方(官员?)

  2. 一个合法的不同的方式重新present for循环的算术击?

  3. 只是我丢失的东西如何正常的算术for循环间preting输入?

P.S。 - 这是在OS X小牛测试既具有自制安装击4.2.45(2)-release以及香草51年2月​​3日(1) - 释放

推荐答案

传输一些意见到一个答案:

Transferring some comments into an answer:

据我所知,这在很大程度上是一个无证的黑客在Bash中,可能需要使用的是Bourne shell也支持括号符号,也没有证件的兼容性。它不是特有的Mac OS X上;它的工作原理一样,在Ubuntu 12.04也。这似乎不符合来工作,而循环或直到循环在Bash中,虽然。

AFAIK, it is a largely undocumented hack in Bash, probably needed for compatibility with the Bourne shell which also supported the brace notation, also undocumented. It is not peculiar to Mac OS X; it works like that on Ubuntu 12.04 too. It doesn't seem to work with while loops or until loops in Bash, though.

你怎么写算术在Bourne shell的语法循环?

这是写循环体的可选途径;病情会在不同的Bourne shell写的,但你可以写的东西像

It is an optional way of writing the body of the for loop; the condition would be written differently in Bourne shell, but you could write things like

for i in 1 2 3
{
    echo $i
}

在Bourne shell中,或者代替使用不要 {完成代替}

in Bourne shell, or use do in place of { and done in place of }.

(我忘了它的存在,直到你的问题让我想起了它。我从来没有用过它当回事,因为它没有记录,因此不能保证被支持)

(I'd forgotten that it existed until your question reminded me of it; I've never used it seriously because it was not documented and hence not guaranteed to be supported.)

做......做结构不能使用其中 {...} 即可使用;你只能使用做......做,而直到循环,而你可以写 {回声喜; } 的任何地方的命令可能出现(如如果{回声喜;};然后回显螺;网络你不能做到这一点与完成

The do ... done construct cannot be used where { ... } can be used; you can only use do ... done after a for, while or until loop, whereas you can write { echo Hi; } anywhere a command could appear (e.g. if { echo Hi; }; then echo Lo; fi. You can't do that with do and done.

要检查这个答案的真实性,最好的办法可能是下载传家宝Bourne Shell中从传家宝项目。

The best way to check the veracity of this answer is likely to be to download the Heirloom Bourne Shell from the Heirloom Project.

我已经下载了源$ C ​​$ C的传家宝Bourne shell的(CVS仓库),并建立了它,并对其进行了测试。它确实接受后for循环的花括号和后拒绝承认他们作为代理人为不要完成 直到,而循环。

I've downloaded the source code for the Heirloom Bourne Shell (a CVS repository) and built it, and tested it. It does indeed accept the braces after the for loop, and refuses to recognize them as surrogates for do and done after an until or while loop.

这篇关于一个bash循环用大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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