开发者

for loop not working

My for loop is not working:

#!/bin/sh
for (( count=2; count < 5; count++))
do
  parameter=$count
  echo开发者_运维百科 $parameter
done

Error:

./new.sh: syntax error at line 2: `(' unexpected


The (( )) construct is not POSIX. You must use an interpreter like #!/bin/bash if you want this.

The POSIX alternative for this would be:

for count in 2 3 4; do
  parameter=$count
  echo $parameter
done

Or

for count in $(seq 2 4); do
  parameter=$count
  echo $parameter
done

The latter being more scalable at the cost of calling an external binary (seq)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜