((COUNT ++)) - > shell脚本中的"COUNT ++:not found"

yay*_*ayu 2 shell

#!/bin/sh
INTERVAL=1
COUNT=0
while [ $COUNT -le 9 ]
do
    (( COUNT++ ))
    sleep $INTERVAL
    echo "count is $COUNT"
done
Run Code Online (Sandbox Code Playgroud)

执行时.

$ sh ascript 
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
count is 0
ascript: 9: COUNT++: not found
Run Code Online (Sandbox Code Playgroud)

Car*_*rum 7

如果你想使用特定于bash的操作,你可能想要#!/bin/bash而不是#!/bin/sh在顶部.

你的脚本在我的Mac上运行正常,这里sh真的很棒bash.如果你sh是一个真正的人,你可能不会那么幸运.


gee*_*aur 7

(( ))将是一个嵌套的子shell(实际上是其中两个),带有一个命令的调用COUNT++.你想要$(( ))算术替代机制; 但这实际上会替代,所以你要么想在评论中隐藏它,要么使用涉及替换的增量.

: $(( COUNT++ )) # : is a shell comment
Run Code Online (Sandbox Code Playgroud)

要么

COUNT=$(( $COUNT + 1 ))
Run Code Online (Sandbox Code Playgroud)