![Makefile 规则中的算术运算](https://linux22.com/image/126434/Makefile%20%E8%A7%84%E5%88%99%E4%B8%AD%E7%9A%84%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97.png)
我需要在 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