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

这个shell循环应该可以工作,不是吗?,shell,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 `]' 我敢肯定这是件愚蠢的事。谢谢大家!

我正在学习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 `]'

我敢肯定这是件愚蠢的事。谢谢大家!

shell对空格非常挑剔。注释是内联的

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

shell对空格非常挑剔。注释是内联的

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

shell对空格非常挑剔。注释是内联的

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

shell对空格非常挑剔。注释是内联的

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

与爪哇和C++不同,在赋值中不能有“代码> >代码/代码>的空间。在代码< > [/COD>和<<代码> > /代码>之前,需要一个空间。最后,<代码> i=Exp$$i+1 \ <代码>应该是<代码> i=$(($i+1))< /代码>。要么,要么使用回退而不是简单引号。我很确定这是BASH或KSH。你应该用适当的shell来标记它。不像爪哇和C++,在赋值中不能有“代码”> = />代码空间。你需要在<代码> [< /代码>和<<代码> > /代码>之后的空间。最后,<代码> 'ExpR$i+1’< /C>应该是<代码> i=$(($i+1))< /代码>。或者使用反勾号而不是简单的引号。我很确定这是bash或ksh。你应该用适当的shell来标记它。不像爪哇和C++,在赋值中不能有“代码”> = />代码空间。你需要在<代码> [< /代码>和<<代码> > /代码>之后的空间。最后,<代码> 'ExpR$i+1’< /C>应该是<代码> i=$(($i+1))< /代码>。或者使用反勾号而不是简单的引号。我很确定这是bash或ksh。你应该用适当的shell来标记它。不像爪哇和C++,在赋值中不能有“代码”> = />代码空间。你需要在<代码> [< /代码>和<<代码> > /代码>之后的空间。最后,<代码> 'ExpR$i+1’< /C>应该是<代码> i=$(($i+1))< /代码>。或者使用反勾号而不是简单的引号。我很确定这是bash或ksh。您应该用适当的shell标记它。