You can do this completely in a shell like this:
ENVDIR := $( command -v envdir 2> /dev/null || ( install envdir > /dev/null && echo newEnvDirPath ) )
This is easiest, but causes the installation of envdir , even if it is not required for the current purpose. If you want it on target, you can do something like:
ENVDIR:=$(cat .ENVDIR 2> /dev/null) $(type -v $(ENVDIR) > /dev/null || rm .ENVDIR ) .ENVDIR: command -v envdir 2> /dev/null > $@ || ( install envdir > /dev/null && echo newEnvDirPath > $@ ) $(eval ENVDIR:=$$(cat $@ )) shell: | .ENVDIR
If the .ENVDIR file .ENVDIR not exist, it will run a recipe that will either write the installed path to .ENVDIR or install and write a new path to .ENVDIR . In any case, when the recipe line ends, .ENVDIR will contain the executable path. The target then runs $(eval) to install envdir . Note that the eval call causes the makefile to redraw, which is not ideal, but should only be done once on the system. (Also note the double $$ before the cat command, which does not allow make to extend this while reading ...).
Alternatively, if .ENVDIR exists, it will read the file to determine which envdir will try to use. The check is performed on the next line to make sure that this version still exists in the system, and if not, it deletes the file, forcing it to be recreated.
source share