Update value of variable

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Update value of variable

icvallejo
This post was updated on .
Hello all, I'm rookie with gnu make and I have a question; I'm doing a Makefile like this:

#!/bin/bash
# Makefile

IMPORT_PATH := github.com/...
CHECK := 0

.PHONY: install
install:
        $Q printf "\\nLet's install..."
#      $Q export CHECK=0
        $Q if grep -q \!string "$(DIR)" ; then CHECK = $($CHECK + 1) ; fi
        $Q printf "done!"



I want that if the condition is good, update the value of CHECK from 0 to 1, but I don't know how can I do this :S

Can you help me please ? Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Uupdate value of variable

Duane Griffin
Hi Ignacio,

I think you are trying to assign to a make variable from a command in
a recipe. You can't do that, sorry. Consider that each line in a
recipe runs in a separate shell: it cannot change variables in it's
parent make process or even set shell variables for other lines in the
same recipe.

To understand how to achieve your goal I think we need to know what
you intend to do with CHECK. Is it really a counter or just a boolean?
Do you just want to fail the build if it fails or do something else?

Cheers,
Duane.

On Thu, Jun 1, 2017 at 1:28 AM, icvallejo
<[hidden email]> wrote:

> Hello all, I'm rookie with gnu make and I have a question; I'm doing a
> Makefile like this:
>
> #!/bin/bash
> # Makefile
>
> IMPORT_PATH := github.com/...
> CHECK := 0
>
> .PHONY: install
> install:
>         $Q printf "\\nLet's install..."
> #             $Q export CHECK=0
>         $Q if grep -q \!string "$(DIR)" ; then CHECK = $($CHECK + 1) ; fi
>         $Q printf "done!"
>
>
>
> I want that if the condition is good, update the value of CHECK from 0 to 1,
> but I don't know how can I do this :S
>
> Can you help me please ? Thank you!
>
>
>
> --
> View this message in context: http://gnu-make.2324884.n4.nabble.com/Uupdate-value-of-variable-tp17777.html
> Sent from the Gnu - Make - Help mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-make mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-make



--
"I never could learn to drink that blood and call it wine" - Bob Dylan

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Uupdate value of variable

Tim Murphy-4
In reply to this post by icvallejo
Hi

On 31 May 2017 at 14:28, icvallejo <[hidden email]>
wrote:

> Hello all, I'm rookie with gnu make and I have a question; I'm doing a
> Makefile like this:
>
> #!/bin/bash
>
^^^^^ This doesn't make any sense - this is not a bash script.


> # Makefile
>
> IMPORT_PATH := github.com/...
> CHECK := 0
>
> .PHONY: install
> install:
>         $Q printf "\\nLet's install..."
> #             $Q export CHECK=0
>         $Q if grep -q \!string "$(DIR)" ; then CHECK = $($CHECK + 1) ; fi
>

^^^^ The assignment here creates a shell variable "CHECK" which doesn't
exist after the recipe finishes and it tries to assign a make variable
called " + 1" into it which also doesn't make sense.


>         $Q printf "done!"
>

You're going to get into trouble here with the difference between make
variables $(VARIABLE) and bash variables $VARIABLE and also with the time
at which recipes are expanded.

Expansion in a recipe usually happens just before the recipe is executed.
You can change the value of a variable (using $(eval))  before the recipe
is run but that's no use to you here because you want the change to depend
on something the recipe does.

It might seem dull but I think you should probably read the GNU make manual
for a while and then post here again with the thing you're trying to
achieve.

Regards,

Tim
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Uupdate value of variable

icvallejo
In reply to this post by Duane Griffin
Hello and thank you for your answers.

I want to know if exists a string (!myapp) in rsyslog.conf file and if there is not exist, check if exists another string (local?.* where ? is a number between 0-7), and to do a printf in file so, for the first:

CHECK := 0
i := 0

.PHONY: syslog
syslog:
        $Q printf "\nLet's install syslog..."
# If the string !myapp exists, CHECK=0 > 1
        $Q $(eval CHECK := $(shell if grep -q \!myapp "/etc/rsyslog.conf" ; then echo $$(( $(CHECK) +1 )) ; else echo $(CHECK) ; fi ) )
# Here, I want to do: if CHECK = 0 (!myapp does not exit), to do a while like this:
        $Q while CHECK=0 && i -le 8 ; do \
              if grep -q local$i.* "/etc/rsyslog.conf" ; then echo $$(( $i +1 )) ; else echo $$(( $(CHECK) + 1 )) && printf "\n\!myapp\nlocal$i.*\t\t\t\t\t/var/log/myapp.log" >> /etc/rsyslog.conf ; fi \
              done
        $Q printf "\ndone!\n\n"

I don't know if it's clear:
First, if exists !myapp, if it does not exist, to do a while to check if exists local?.* where ? is a number between 0-7. At the moment that local?.* does not exist, take it, to do a printf and exit of while. Is it possible ?

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Uupdate value of variable

Duane Griffin
Hi,

On Thu, Jun 1, 2017 at 7:24 PM, icvallejo
<[hidden email]> wrote:
> Hello and thank you for your answers.
>
> I want to know if exists a string (!myapp) in rsyslog.conf file and if there
> is not exist, check if exists another string (local?.* where ? is a number
> between 0-7), and to do a printf in file so, for the first:

It doesn't sound like make is really the right tool for the job.
You're just going to end up with a single rule that always runs and
does all the work with an inline shell command. It would be better to
just write and call a shell script instead: make really does nothing
for you in this case.

> # If the string !myapp exists, CHECK=0 > 1
>         $Q $(eval CHECK := $(shell if grep -q \!myapp "/etc/rsyslog.conf" ; then

You are still trying to assign a makefile variable from within a
recipe. You can't do that.

> First, if exists !myapp, if it does not exist, to do a while to check if
> exists local?.* where ? is a number between 0-7. At the moment that local?.*
> does not exist, take it, to do a printf and exit of while. Is it possible ?

To make this work CHECK and i need to be shell variables, not makefile
variables. As I said, you're going to end up doing all the work in a
shell command so just write a shell script. Or, better, look into
tools designed for this job such as augeas or ansible, etc.

Cheers,
Duane.

--
"I never could learn to drink that blood and call it wine" - Bob Dylan

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: Uupdate value of variable

icvallejo
Thank you Duane,

Finally I will do that with the main program writes in golang and the Makefile will be more simple.

Regards.