开发者

for loop not working

开发者 https://www.devze.com 2023-02-22 21:44 出处:网络
My for loop is not working: #!/bin/sh for (( count=2; count < 5; count++)) do parameter=$count echo开发者_运维百科 $parameter

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

精彩评论

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