Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If not exists, create a folder – Error in makefile

I searched over the internet for a command to create a folder if it doesn’t exist. I found it and put in my makefile

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
    [ ! -d ${folder} ] && mkdir -p ${folder}

nextstep:
    echo "Got here!"

all: createfolder nextstep

When the folder doesn’t exist it’s created correctly. But I get an error if the folder already exists.

$ ls
makefile
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
echo "Got here!"
Got here!
$ ls
makefile  myfolder
$ make
[ ! -d "myfolder" ] && mkdir -p "myfolder"
make: *** [makefile:5: createfolder] Error 1

I don’t get why the command would give an error if the condition [ ! -d "myfolder" ] is before the mkdir and it shouldn’t even execute the second command.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I solve it?

>Solution :

You can use if then fi syntax

.DEFAULT_GOAL := all
folder := "myfolder"

createfolder:
        if [ ! -d ${folder} ]; then mkdir -p ${folder}; fi

nextstep:
        echo "Got here!"

all: createfolder nextstep
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading