Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile : how to increment a variable when you call it? (var++ in bash)

Here is part of my makefile :

LISTEINC = $(DEST)/file.inc $(DEST)/otherfile.inc $(DEST)/anotherfile.inc
compteur = 1

$(DEST)/file: $(LISTEINC)
       #action

$(DEST)/%.inc: $(DEST)/%.jpg
      ./script $< $compteur $(DEST) > $@

How to have the variable compteur at 1 for file, 2 for otherfile, 3 for anotherfile?

$((compteur++)) would work in bash script, but here I really don't know what the equivalent is. I tried many combination of $$ () ++ +1 etc... Nothing worked. Anyone could help me please?

like image 590
Dreamk33 Avatar asked Dec 07 '15 20:12

Dreamk33


People also ask

How do you increment a variable in bash?

Increment Bash Variable with + Operator The bash variable can be incremented by summing it with the 1. By default, the + operator has a different meaning than the sum for the bash shell. The $((…)) operators are used to express the + operator is used arithmetic operation.

Can you do += in bash?

In addition to the basic operators explained above, bash also provides the assignment operators += and -= . These operators are used to increment/decrement the value of the left operand with the value specified after the operator.

What is an increment variable?

Incremental Variables. Incremental variables allow you to generate a sequence of numbers or dates to use in user scenarios. Each incremental variable has an initial value and an increment step and updates its value at certain times (for example, on each use).


1 Answers

It can be done with eval :

$(eval compteur=$(shell echo $$(($(compteur)+1))))

From the manual :

The eval function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax. The expanded results can define new make variables, targets, implicit or explicit rules, etc

like image 66
Bertrand Martel Avatar answered Sep 29 '22 08:09

Bertrand Martel