linux - bash 中的简单 while 循环

标签 linux bash while-loop

我已经开始学习 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 or man [ 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/

相关文章:

c++ - CMake 中的 OpenCV 和 Gtest 冲突

linux - 匹配时如何退出 Sed

c++ - 是否建议在 bash 脚本中捕获 SIGPIPE?

linux - 嵌套 while 循环(linux 中的 bash 脚本)

Java程序: Where am I going wrong?

linux - Bash:在 .txt 文件中搜索和替换密码

c - 带有 setuid/capabilities 的 stdbuf

linux - 使用 Wayland/Weston 列出屏幕分辨率

linux - 使用 Last 和 tr 剪切行和列

java - Selenium 和Java : While loop not working as expected