Conditionally executing a script and using return value

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

Conditionally executing a script and using return value

Thomas Nyberg
Hello,

I would like my Makefile to:

        1. Check if a variable is set to specific value,
        2. If it is, execute a script and get its return value,
        3. Stop executing Makefile depending upon return value.

Here is the Makefile I have:

`Makefile`
-------------------------------------------------------------
     FORCE=true

     all: check_force
         @echo "success"

     check_force:
         ifeq ($(FORCE),true)
         $(shell sh ./prompt.sh)
         endif
-------------------------------------------------------------

and here is the `prompt.sh` file:

`prompt.sh`
-------------------------------------------------------------
     while true;
     do
         read -p "Are you sure you want force to be \"true\"? (y/n) " resp
         case $resp in
             [Yy]* ) echo "You picked yes"; exit 0;;
             [Nn]* ) echo "You picked no"; exit 1;;
             * ) echo "You picked nonsense";;
         esac
     done
-------------------------------------------------------------

I have tried many different variations of this with various
errors/problems, but basically I would like to stop execution if FORCE
is set to "true" and if the user selects "n" in the prompt. If the user
selects "y" I would like things to continue (in this case simply echo
"success"). If FORCE is not set to "true", I don't want the script to
execute at all.

Thanks for any help! Ask for more details if I'm not making sense...

Cheers,
Thomas

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

Re: Conditionally executing a script and using return value

Kaz Kylheku (gmake)
On 2019-04-11 03:04, Thomas Nyberg wrote:

> Hello,
>
> I would like my Makefile to:
>
> 1. Check if a variable is set to specific value,
> 2. If it is, execute a script and get its return value,
> 3. Stop executing Makefile depending upon return value.
>
> Here is the Makefile I have:
>
> `Makefile`
> -------------------------------------------------------------
>     FORCE=true
>

Missing:

.PHONY: all check_force

so that make doesn't probe the filesystem looking for
targets named "all" and "check_force" (and refuse to build anything if
they both exist and "all" is newer).

>     all: check_force
>         @echo "success"
>
>     check_force:
>         ifeq ($(FORCE),true)

ifeq lines cannot be indented with a tab. Tabbed lines
are strictly recipe lines.

>         $(shell sh ./prompt.sh)

This should just be: ./prompt.sh   since it's just a step
in the recipe; $(shell ...) is usually used outside of recipes for
obtaining the effect of executing a shell command while Makefile syntax
is being read, and substituting its output.

In a recipe line if we do  $(shell echo foo),  make will execute "echo
foo" during the expansion of the recipe line and capture the "foo"
output which will then be integrated into the expanded recipe line. Your
recipe line here is saying, "take the output of sh ./prompt.sh and
execute it as a command, failing the recipe if that command fails".

>         endif

we need an unconditionally successful recipe if the
variable isn't "true", rather than a blank recipe:

else
         exit 1
endif


Cheers ...

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make