bash中的简单while循环 [英] simple while loop in bash

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

问题描述

我已经开始学习bash脚本.我编写了简单的while循环,但是没有用.有人说:找不到命令.有人知道为什么吗?这是我的代码:

I've started learning bash scripting. I wrote simple while loop, but it doesn't work. it's say that : command not found.does anybody knows why ? Here is my code:

let x=5; while [$x -lt 10];do echo "x is : $x";let x=$x+1; done

推荐答案

添加空格.

while [ $x -lt 10 ];

有关更多信息,请参见如何使用双括号或单括号,括号,花括号的答案:

For more information, please see this answer to How to use double or single bracket, parentheses, curly braces:

一个方括号( [)通常实际上调用一个名为 []的程序; man测试 man [以获得更多信息.示例:

A single bracket ([) usually actually calls a program named [; man test or man [ for more info. Example:

$ VARIABLE=abcdef
$ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi
yes

此外,这是信息测试在此问题上必须说的:

Also, this is what info test has to say on the matter:

' test '具有使用开合正方形的另一种形式方括号代替前导的" test ".例如,代替' test -d/",则可以编写" [-d/] ".方括号必须分开论点例如,' [-d/] '效果不理想.由于" test EXPR "和" [EXPR] "的含义相同,因此仅前一种形式将在下面讨论.

'test' has an alternate form that uses opening and closing square brackets instead a leading 'test'. For example, instead of 'test -d /', you can write '[ -d / ]'. The square brackets must be separate arguments; for example, '[-d /]' does not have the desired effect. Since 'test EXPR' and '[ EXPR ]' have the same meaning, only the former form is discussed below.

因此,等效项如下所示:

Therefore, the equivalent would look like:

let x=5; while test $x -lt 10;do echo "x is : $x";let x=$x+1; done

这篇关于bash中的简单while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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