这个shell循环应该可以工作,不是吗?

时间:2015-03-18 19:57:15

标签: shell

我正在学习shell编程(来自Java和C ++)。我在一个名为loop的文件中有这个循环:

read count
i = 1
while [$i -le $count]
do
    echo This is loop $i of $count
    i='expr $i + 1'
done

它读取输入,但出现以下错误:

./loop: line 2: i: command not found
./loop: line 3: [: missing `]'

我很确定这是愚蠢的事。谢谢!

1 个答案:

答案 0 :(得分:3)

关于空白的贝壳非常挑剔。评论是内联的。

read count
i=1                                 # spaces not allowed
while [ $i -le $count ]             # spaces required around brackets
do
    echo This is loop $i of $count
    i=$(expr $i + 1)                # need to use $(...) or `...` not '...'
done