简体   繁体   English

循环运行Shell脚本时出错

[英]Error in running shell script while loop

I wrote this shellscript file. 我写了这个shellscript文件。 But I get error near unexpected token done'` 但是我error near unexpected token完成error near unexpected token得到了error near unexpected token '

#!/bin/bash
i=1
while [ $i -lt 12 ]; do
    echo Hi
    i=$[$i+1]
done

Previously, there was no ; 以前没有; before do . do之前。 I read stackexchange answers and wrote ; 我读了stackexchange的答案并写道; after while [] , still I get error. 经过while [] ,仍然出现错误。 I could not found the resolution online. 我无法在线找到分辨率。 Any ideas? 有任何想法吗?

i=$[$i+1] this is wrong. i=$[$i+1]这是错误的。 You probably meant i=$((i+1)) 您可能是说i=$((i+1))

bash的路径不正确,您错过了将路径更改第一行的根

#!/bin/bash

Correct placement of ; 正确放置; in a bash script can be tricky indeed. 在bash脚本中确实很棘手。 Here's a one-liner version of your script with semicolons in the right place: 这是脚本的单线版本,在正确的位置带有分号:

i=1; while (( i < 12 )); do echo "Hi"; (( i++ )); done

Note that the $ in variables becomes redundant for integer comparisons if placed between double parentheses (( )) . 请注意,如果将$ in变量放在双括号(( ))之间,则对于整数比较而言将变得多余。

Works perfectly 完美运作

$ ./ttt
Hi
Hi
Hi
Hi 
Hi
Hi
Hi
Hi
Hi
Hi 
Hi
$ cat ttt
#!/bin/bash
i=1
while [ $i -lt 12 ]; do
    echo Hi
    i=$[$i+1]
done  
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM