我需要在 bash 循环中执行算术运算,如下所述
CYCLE?=3
COUNT=1
download_when_ready: ## Will try the download operations many times till it succeeds or it reaches 10 tries
while ! composer update $(bundle) 2> /dev/null && [[ $(COUNT) -lt 10 ]]; \
do \
COUNT=$$(( $(COUNT)+1 )); \
SLEEP=$$(( ($(COUNT) / $(CYCLE)) + ($(COUNT) % $(CYCLE)) )); \
echo "count $(COUNT)"; \
echo "cycle $(CYCLE)"; \
echo "sleep $(SLEEP)"; \
sleep $(SLEEP); \
done
这永远不会停止并给出以下内容:
count 0
cycle 4
sleep 0
count 0
cycle 4
sleep 0
....
count 0
cycle 4
sleep 0
如您所见,变量具有初始值并且永远不会改变!
更新
PRETTY_NAME="SUSE Linux Enterprise Server 11 SP4"
但是,以下代码$$c
在 while 循环之前和内部保持空值。
CYCLE?=3
COUNT=1
download_when_ready: ## Will try the download operations many times till it succeeds or it reaches 10 tries
@c=$(COUNT);
@echo $$c;
while ! composer update $(bundle) 2> /dev/null && [[ $(COUNT) -lt 10 ]]; \
do \
echo "$$c"; \
done
更新
感谢@Kusalananda 的评论,我明白了。
我使用make变量作为shell变量的初始值
这确实有效!
感谢@Kusalananda和@Stéphane Chazelas