Makefile 规则中的算术运算

Makefile 规则中的算术运算

我需要在 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 企业服务器 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

答案1

更新

谢谢@Kusalananda 评论, 我想到了。

我用了变量作为初始值变量

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 && [ "$$c" -lt 10 ]; \
    do \
        c=$$(( $${c:-$(COUNT)}+1 )); \
        s=$$(( ($$c / $(CYCLE)) + ($$c % $(CYCLE)) )); \
        echo "count $$c"; \
        echo "cycle $(CYCLE)"; \
        echo "sleep $$s"; \
        sleep $$s; \
    done

这确实有效!

count 1
cycle 4
sleep 1
count 2
cycle 4
sleep 2
count 3
cycle 4
sleep 3
count 4

谢谢@Kusalananda&@斯特凡·查泽拉斯

相关内容