I want to make a counter for a while loop in my makefile. Tell me what I am doing wrong, why is the number in COUNTER not growing? start.s :
<------>@$(EECHO) Starting server...
<------>COUNTER=0
<------>@while [ ! -f ${CS_LOG_FILE} ]; do \
<------> echo Wait for the start server... ; \
<------> sleep 1 ; \
<------> COUNTER = COUNTER + 1 : \
<------> echo ${COUNTER} ; \
<------> $(if $(shell if [ "$(COUNTER)" != "10" ]; then \
<------> echo "stop"; fi), $(error it seems that '${NAME}' is not found in ${CS_LOG_FILE})) \
<------>done
<------>@$(EECHO) Starting server completed...
Many problems here.
There is a fundamental misunderstanding: you are trying to mix in make functions and variables and shell operations. The two don't run at the same time. They run serially: FIRST all the make functions and variables are expanded, THEN the shell is invoked on the results of that expansion and it runs in a totally separate process.
So, a shell construct like a while
loop cannot set, or use, make functions like if
, error
, etc. because by the time the shell loop is running ALL the make functions and variables were expanded and the shell is running somewhere else.
So in your example, make will expand the string you provided FIRST, which gives a result of:
while [ ! -f somefile ]; do \
echo Wait for the start server... ; \
sleep 1 ; \
COUNTER = COUNTER + 1 : \
echo ; \
$(error it seems that '${NAME}' is not found in ${CS_LOG_FILE})) \
done
which of course will fail when it expands the error
function, before the shell is even invoked.
You have to write the entire thing as a shell script. You have to use shell constructs, not make constructs. Remember that every $
you want to pass to the shell must be doubled, $$
, to escape it from make. Something like:
@$(EECHO) Starting server...
@COUNTER=0; \
while [ ! -f ${CS_LOG_FILE} ]; do \
echo Wait for the start server... ; \
sleep 1 ; \
COUNTER=$$(expr $$COUNTER + 1) ; \
echo $$COUNTER ; \
if [ $$COUNTER -eq 10 ]; then \
echo it seems that '${NAME}' is not found in ${CS_LOG_FILE}; \
exit 1; \
fi; \
done
@$(EECHO) Starting server completed...