我已经开始学习 bash 脚本。我写了简单的 while 循环,但它不起作用。 它说:找不到命令。有人知道为什么吗? 这是我的代码:
let x=5; while [$x -lt 10];do echo "x is : $x";let x=$x+1; done
最佳答案
添加空格。
while [ $x -lt 10 ];
有关更多信息,请参阅对 How to use double or single bracket, parentheses, curly braces 的回答:
A single bracket (
[
) usually actually calls a program named[
;man test
orman [
for more info. Example:$ VARIABLE=abcdef $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; fi yes
此外,这就是 info test
对此事的看法:
'
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.
因此,等效项如下所示:
let x=5; while test $x -lt 10;do echo "x is : $x";let x=$x+1; done
关于linux - bash 中的简单 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713594/