while循环在shell脚本中不能在linux bash shell中工作

问题描述:

在linux中编写shell脚本的新手是一种新手。它是一个csh脚本,但我在bash shell中运行它,为什么我使用#!/bin/bash而不是#!/bin/cshwhile循环在shell脚本中不能在linux bash shell中工作

1 #!/bin/bash 
    2 set i = 1 
    3 echo it starts 
    4 
    5 while ($i <= 5) 
    6   echo i is $i 
    7   @ i= $i +1 
    8 end 

**注意:**数字只是代表行数。

上面的代码给我错误的输出:

it starts 
./me.csh: line 9: syntax error: unexpected end of file 

我无法弄清楚什么是错的,即使它回声it starts并没有为错误中指定没有行号9。

+2

你的问题标签,标题和家当说'bash',但你的脚本'csh'。 'bash'和'csh'是两种不同的语言。你打算用哪种语言编写脚本? –

+0

我打算写'csh',但是我正在从'bash'外壳运行它 –

+0

坦率地说,我会鼓励你用bash堆栈... – paulsm4

的家当必须设置应该解释壳剧本。你从哪个shell运行脚本并不重要。唯一重要的是剧本写入的语言。

你的脚本写在csh,因此必须有shebang #!/bin/csh。即使你想从bash运行它也是如此。此外,你在你的at符号,包换缺少空间:

$ cat me.csh 
#!/bin/csh 
set i = 1 
echo it starts 

while ($i <= 5) 
     echo i is $i 
     @ i = $i + 1 
end 

输出:

$ ./me.csh 
it starts 
i is 1 
i is 2 
i is 3 
i is 4 
i is 5 
+0

谢谢,但我得到这个错误:'bash:./me.csh:/ bin/csh:糟糕的解释器:没有这样的文件或目录。而我使用Kali linux,我不知道它是否没有'csh'。 –

+0

如果它没有csh,则必须安装csh或将脚本重写为您支持的语言。 –

+0

我安装了'csh',现在它使用'#!/ bin/csh'工作。感谢您的想法。 –

试试这个:

#!/bin/bash 
echo it starts 

i=1 
while [ $i -le 5 ]; do 
    echo i is $i 
    i=$((i+1)) 
done 

输出示例:

it starts 
i is 1 
i is 2 
i is 3 
i is 4 
i is 5 

这里是一个很好的参考:

BASH Programming - Introduction HOW-TO

+0

很抱歉,但也许我没有更具体。我编辑了我的问题。 –